DataGrid不在页面上显示

时间:2015-07-16 09:36:45

标签: c# asp.net visual-studio datagrid

我正在尝试创建一个DataGrid,但是当我加载页面时它没有显示。我正在尝试为DataGrid创建一个名为BuildGrid的函数。

protected void LoadJob(int JobID)
{
        DataGrid db = JobClass.Job_Piece.BuildGrid();

        db.DataSource = jb.Pieces;
        db.DataBind();
}

所以BuildGrid函数是:

public static DataGrid BuildGrid()
        {
            DataGrid NewDg = new DataGrid();

            NewDg.DataKeyField = "ID";
            NewDg.AutoGenerateColumns = false;
            NewDg.CssClass = "tblResults";
            NewDg.HeaderStyle.CssClass = "tblResultsHeader";
            NewDg.AlternatingItemStyle.CssClass = "ResultsStyleAlt";
            NewDg.ItemStyle.CssClass = "ResultsStyle";


            NewDg.Columns.Add(Load.CreateBoundColumn("AdvisedQty", "No Of Pces"));
            NewDg.Columns.Add(Load.CreateBoundColumn("PieceTypeString", "Pces Type"));
            NewDg.Columns.Add(Load.CreateBoundColumn("ReceivedQty", "Rcvd Pces"));
            NewDg.Columns.Add(Load.CreateBoundColumn("Reason", "Reason"));

            NewDg.Width = Unit.Percentage(100.00);

            return NewDg;
        }

1 个答案:

答案 0 :(得分:1)

当然它不会在页面上显示...因为你在codebehind文件中创建了datagrid但你没有将创建的网格添加到页面即html ..你需要在创建动态页面时动态地添加创建的数据网格

要添加的代码

   // DataGrid1 is added to the PlaceHolder
   PlaceHolder.Controls.Add(DataGrid1);

aspx页面代码

<form id="Form1" method="post" runat="server">
  <asp:PlaceHolder id="PlaceHolder" runat="server" />
</form>

一旦您在aspx页面中填写了掌上电脑,您的代码隐藏就会

protected void LoadJob(int JobID) {         DataGrid db = JobClass.Job_Piece.BuildGrid();

    db.DataSource = jb.Pieces;
    db.DataBind();
    PlaceHolder.Controls.Add(db);

}