将第二个标题行添加到GridView BELOW已存在的标题行

时间:2012-07-24 21:19:55

标签: asp.net .net gridview code-behind

我正在尝试在GridView的OnRowCreated事件中动态插入第二个标题行。但是,除了gridview的行索引中的第一个点之外,我似乎无法将行插入任何位置。下面的代码在最后一行失败,其中标题实际上被添加到网格中。索引越界异常。如何在已存在的标题行中添加此标题行 ?非常感谢帮助,谢谢!

protected void gvwProd_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        GridView gvw = (GridView)sender;

        GridViewRow HeaderRow = new GridViewRow(1, 1, DataControlRowType.Header, DataControlRowState.Insert);

        TableCell HeaderCell = new TableCell();
        HeaderCell.Text = "Prod Comments - TS/LID";
        HeaderCell.Style["font-weight"] = "bold";
        HeaderCell.ColumnSpan = 4;
        HeaderCell.Wrap = false;
        HeaderRow.Cells.Add(HeaderCell);

        HeaderCell = new TableCell();
        HeaderCell.Text = "Materials Comments - TS/LD";
        HeaderCell.Style["font-weight"] = "bold";
        HeaderCell.ColumnSpan = 8;
        HeaderCell.Wrap = false;
        HeaderRow.Cells.Add(HeaderCell);

        HeaderCell = new TableCell();
        HeaderCell.Text = "Plant Comments - TS/LID";
        HeaderCell.Style["font-weight"] = "bold";
        HeaderCell.ColumnSpan = 11;
        HeaderCell.Wrap = false;
        HeaderRow.Cells.Add(HeaderCell);

        gvw.Controls[0].Controls.AddAt(1, HeaderRow);
    }
}

1 个答案:

答案 0 :(得分:0)

有类似的问题。您可以尝试将GridView转换为表并访问.Rows方法:

((Table)gvw.Controls[0]).Rows.AddAt(1, HeaderRow);

source