Gridview Add Columns Dynamically Using Code Behind

code-behindgridview

I'm trying to create columns dynamically with C# code behind, I need to create a column with Textboxes, how can I go on doing that? Here's my code:

protected void loadTable()
{
    gv_productDesc.Columns.Clear();
    SqlConnection con = new SqlConnection();
    con.ConnectionString = ConfigurationManager.ConnectionStrings["UserCS"].ConnectionString;

    string sql = "SELECT * FROM ItemTable WHERE categoryId=@pcategoryId";
    SqlCommand cmd = new SqlCommand(sql, con);

    cmd.Parameters.AddWithValue("@pcategoryId", ddl_category.SelectedValue);

    SqlDataReader dr;
    DataTable dt = new DataTable();

    try
    {
        con.Open();
        dr = cmd.ExecuteReader();
        dt.Load(dr);

        /*BoundField itemname = new BoundField();
        itemname.HeaderText = "Item Name";
        itemname.HeaderStyle.CssClass = "gridPadding";
        itemname.DataField = "itemName";
        itemname.ItemStyle.CssClass = "gridPadding";
        itemname.ItemStyle.Font.Bold = true;
        itemname.ItemStyle.Width = 225;
        gv_productDesc.Columns.Add(itemname);*/

        ButtonField btnfield = new ButtonField();
        btnfield.HeaderText = "Item Name";
        btnfield.HeaderStyle.CssClass = "gridPadding";
        btnfield.ButtonType = ButtonType.Link;
        btnfield.DataTextField = "itemName";
        btnfield.CommandName = "lala";
        btnfield.ItemStyle.CssClass = "linkcss";
        btnfield.ItemStyle.Width = 225;
        gv_productDesc.Columns.Add(btnfield);

        BoundField itemprice = new BoundField();
        itemprice.HeaderText = "Item Price ($)";
        itemprice.HeaderStyle.CssClass = "gridPadding";
        itemprice.DataField = "unitPrice";
        itemprice.ItemStyle.CssClass = "gridPadding";
        itemprice.ItemStyle.Font.Bold = true;
        itemprice.ItemStyle.Width = 100;
        gv_productDesc.Columns.Add(itemprice);

        BoundField itemdescHidden = new BoundField();
        itemdescHidden.HeaderText = "Item Description";
        itemdescHidden.DataField = "description";
        itemdescHidden.HtmlEncode = false;
        itemdescHidden.ItemStyle.Width = 0;
        gv_productDesc.Columns.Add(itemdescHidden);

        BoundField itemdesc = new BoundField();
        itemdesc.HeaderText = "Item Description";
        itemdesc.HeaderStyle.CssClass = "gridPadding";
        itemdesc.ItemStyle.CssClass = "gridPadding";
        itemdescHidden.DataField = "description";
        itemdesc.HtmlEncode = false;
        itemdesc.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
        itemdesc.ItemStyle.Width = 320;
        gv_productDesc.Columns.Add(itemdesc);

        ImageField itemimage = new ImageField();
        itemimage.HeaderText = "Item Image";
        itemimage.HeaderStyle.CssClass = "gridPadding";
        itemimage.DataImageUrlField = "image";
        itemimage.ItemStyle.CssClass = "gridPadding";
        itemimage.ItemStyle.Width = 225;
        itemimage.ControlStyle.Width = 225;
        gv_productDesc.Columns.Add(itemimage);



        gv_productDesc.DataSource = dt;
        gv_productDesc.DataBind();
        gv_productDesc.Columns[2].Visible = false;
    }
    catch (Exception ex)
    {
        lb_msg2.Text = "Error Encountered : " + ex.Message;
    }
    finally
    {
        con.Close();
        con.Dispose();
        cmd.Dispose();
    }
}

I tried to add the column to the GridView in the main page (example.aspx), but when I bind the values to the GridView, that column disappeared.

Best Answer

Data binding to a GridView will always redraw the GridView based on the columns the data source returns. In your case, you either need to add the columns as part of your data source, or set AutoGenerateColumns="False"

UPDATE

Place your dynamic controls AFTER the code that binds the datatable to the gridview, as per my original comment above.