1

我正在尝试在 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);
    }
}
4

1 回答 1

0

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

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

资源

于 2017-07-17T14:09:40.310 回答