当前位置:编程学习 > C#/ASP.NET >>

GridView增加列的问题

在页面中使用 GridView.Columns.Insert(Index,DataControlField) 动态的给GridView添加列,

但是里面的参数DataControlField写什么东西啊?同时给这个新增的列怎么设置数据绑定字段(DataTextField)? --------------------编程问答-------------------- 还在使用 1.1,不过照以前的方法,需要在对应的 DataTable 增加对应的 Field --------------------编程问答-------------------- <%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            CreateGridView();
        }
    }

    void CreateGridView()
    {
        GridView grd = new GridView();        
        hldContent.Controls.Add(grd);
        grd.AutoGenerateColumns = false;
        
        BoundField boundField = new BoundField();
        grd.Columns.Add(boundField);
        boundField.HeaderText = "产品名称";
        boundField.DataField = "ProductName";                
        
        LoadProductData(grd);
    }

    void LoadProductData(GridView grd)
    {
        DataTable dt = CreateProductTable();
        
        grd.DataSource = dt;
        grd.DataBind();
    }

    #region sample data

    static DataTable CreateProductTable()
    {
        DataTable tbl = new DataTable("Products");

        tbl.Columns.Add("ProductID", typeof(int));
        tbl.Columns.Add("ProductName", typeof(string));
        tbl.Columns.Add("CategoryID", typeof(int));
        DataRow row = tbl.NewRow();
        row[0] = 1;
        row[1] = "Chai";
        row[2] = 1;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 2;
        row[1] = "Chang";
        row[2] = 1;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 3;
        row[1] = "Aniseed Syrup";
        row[2] = 2;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 4;
        row[1] = "Chef Anton's Cajun Seasoning";
        row[2] = 2;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 5;
        row[1] = "Chef Anton's Gumbo Mix";
        row[2] = 2;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 47;
        row[1] = "Zaanse koeken";
        row[2] = 3;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 48;
        row[1] = "Chocolade";
        row[2] = 3;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 49;
        row[1] = "Maxilaku";
        row[2] = 3;
        tbl.Rows.Add(row);

        return tbl;
    }

    #endregion

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:PlaceHolder ID="hldContent" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>
--------------------编程问答-------------------- BoundField bf = new BoundField();
        bf.DataField = "d";//数据源里的字段
        bf.HeaderText = "D";//GridView列名
        GridView1.Columns.Insert(3,bf);
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,