如何在gridview中添加标题,我已编写代码但无法正常工作

时间:2015-01-27 09:42:51

标签: asp.net

我想在网格视图上方添加一个标题。我写了代码,但标题没有显示

protected void GVKeywordReport_RowCreated(object sender, GridViewRowEventArgs e)
{
    GridView HeaderGrid = (GridView)sender;
    GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
    TableCell HeaderCell = new TableCell();
    HeaderCell.Text = "hjhjhjh";
    HeaderCell.ColumnSpan = 10;
    HeaderGridRow.Cells.Add(HeaderCell);
}

2 个答案:

答案 0 :(得分:0)

将HeaderGridRow添加到Gridview:

 GridView1.Controls[0].Controls.AddAt(0, HeaderGridRow );

答案 1 :(得分:0)

我在数据行绑定中写了代码

 protected void GVKeywordReport_RowDataBound(object sender, GridViewRowEventArgs e)


         {
      if (e.Row.RowType == DataControlRowType.Header)
                {
                    int NumCells = e.Row.Cells.Count;
                    for (int i = 0; i < NumCells - 1; i++)
                    {
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
                    }
                }
        }