Gridview使用代码隐藏动态添加列

时间:2012-01-23 09:23:43

标签: gridview code-behind

我正在尝试使用C#代码动态创建列,我需要创建一个包含Textboxes的列,我该如何继续这样做?这是我的代码:

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();
    }
}

我尝试将列添加到主页面中的GridView(example.aspx),但是当我将值绑定到GridView时,该列消失了。

1 个答案:

答案 0 :(得分:0)

与GridView的数据绑定将始终根据数据源返回的列重绘GridView。在您的情况下,您需要将列添加为数据源的一部分,或者设置AutoGenerateColumns="False"

<强> 更新

根据我上面的原始评论,将动态控件 AFTER 放置在将数据表绑定到gridview的代码上。