C# 动态设置Gridview的标题行文本

C# 动态设置Gridview的标题行文本,c#,asp.net,gridview,C#,Asp.net,Gridview,大家好,我有一个函数,它可以动态创建GridView并将其添加到页面中。 数据源也是使用datatable动态设置的。我想将标题行文本设置为datatable的tablename。但以下措施不起作用: private void AddGridview(DataTable dt) { if (dt.Rows.Count > 0) { GridView gridView = new GridView(); gridView.CssClass =

大家好,我有一个函数,它可以动态创建GridView并将其添加到页面中。 数据源也是使用datatable动态设置的。我想将标题行文本设置为datatable的tablename。但以下措施不起作用:

private void AddGridview(DataTable dt)
{
    if (dt.Rows.Count > 0)
    {
        GridView gridView = new GridView();
        gridView.CssClass = "gridview";
        gridView.DataSource = dt;
        gridView.AutoGenerateColumns = false;
        gridView.ShowHeader = true;
        gridView.HeaderRow.Cells[0].Text = dt.TableName;
        remittance.Controls.Add(gridView);
    }
}
它在第39行抛出以下错误:

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 37:             gridView.AutoGenerateColumns = false;
Line 38:             gridView.ShowHeader = true;
Line 39:             gridView.HeaderRow.Cells[0].Text = dt.TableName;
Line 40:             remittance.Controls.Add(gridView);
Line 41:         } 
有什么办法吗


提前感谢。

您需要先添加列

    foreach(var col in table.Columns)
      gridview1.Columns.add(table.ColumnName);

您需要先添加列

    foreach(var col in table.Columns)
      gridview1.Columns.add(table.ColumnName);
而不是

gridView.HeaderRow.Cells[0].Text = dt.TableName;
用这个

gridView.Columns[0].HeaderText = dt.TableName;
更新:

private void AddGridview(DataTable dt)
{
    if (dt.Rows.Count > 0) {
        GridView gridView = new GridView();
        gridView.CssClass = "gridview";
        gridView.DataSource = dt;
        gridView.DataBind();
        gridView.AutoGenerateColumns = false;
        gridView.HeaderRow.Visible = false;

        Table table = gridView.Controls(0);
        GridViewRow gvRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
        TableCell newCell = new TableCell();

        newCell.ColumnSpan = dt.Columns.Count - 1;
        newCell.Text = dt.TableName;

        gvRow.Cells.Add(newCell);
        table.Rows.AddAt(0, gvRow);
        remittance.Controls.Add(gridView);
    }
}
而不是

gridView.HeaderRow.Cells[0].Text = dt.TableName;
用这个

gridView.Columns[0].HeaderText = dt.TableName;
更新:

private void AddGridview(DataTable dt)
{
    if (dt.Rows.Count > 0) {
        GridView gridView = new GridView();
        gridView.CssClass = "gridview";
        gridView.DataSource = dt;
        gridView.DataBind();
        gridView.AutoGenerateColumns = false;
        gridView.HeaderRow.Visible = false;

        Table table = gridView.Controls(0);
        GridViewRow gvRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
        TableCell newCell = new TableCell();

        newCell.ColumnSpan = dt.Columns.Count - 1;
        newCell.Text = dt.TableName;

        gvRow.Cells.Add(newCell);
        table.Rows.AddAt(0, gvRow);
        remittance.Controls.Add(gridView);
    }
}

列已经存在于datatable中,当我将datasource设置为datatable时,所有列都会出现在datatable中。当我将datasource设置为datatable时,所有列都会出现在datatable中。我希望设置整个gridview的标题,而不是列。我尝试了以下操作,但在Table处出现转换错误=控件(0);你确定你复制的正确吗?我刚刚测试了代码,它运行正常。编辑了我的文章,包括了整个函数。我100%确定,因为在那一行,它说“不能隐式地将类型控件转换为表”。我需要显式地施法吗?嗯,试试看。我在VB.NET中尝试过,效果很好,只是将代码转换为C#我想设置整个gridview的标题,而不是列。我尝试了以下操作,但在Table=gridview.Controls(0)处出现转换错误;你确定你复制的正确吗?我刚刚测试了代码,它运行正常。编辑了我的文章,包括了整个函数。我100%确定,因为在那一行,它说“不能隐式地将类型控件转换为表”。我需要显式地施法吗?嗯,试试看。我在VB.NET中尝试过,效果很好,只是将代码转换为C#gridView.HeaderRow.Cells[0]。Text=dt.TableName;这不起作用,可能是因为您尚未调用数据绑定方法。gridView.DataBind();我甚至尝试过,但现在它没有显示错误,也没有设置headergridView.HeaderRow.Cells[0]。Text=dt.TableName;这不起作用,可能是因为您尚未调用数据绑定方法。gridView.DataBind();我甚至试过,但现在它并没有显示错误,也并没有设置标题