0

我正在动态添加表格标题行,如下所示。它被正确渲染。所以现在我有2个标题。

protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.Header)
{
    GridViewRow newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

    TableCell cell1 = new TableHeaderCell();
    cell1.ColumnSpan = 1; //e.Row.Cells.Count;
    cell1.Text = "Expected";

    TableCell cell2 = new TableCell();
    cell2.ColumnSpan = 2;
    cell2.Text = "One";

    TableCell cell3 = new TableCell();
    cell3.ColumnSpan = 2;
    cell3.Text = "Two";

    TableCell cell4 = new TableCell();
    cell4.ColumnSpan = 2;
    cell4.Text = "Three";

    newHeaderRow.Cells.Add(cell1);
    newHeaderRow.Cells.Add(cell2);
    newHeaderRow.Cells.Add(cell3);
    newHeaderRow.Cells.Add(cell4);

    ((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
}

}

如果我的 gridview 有 3 行,并循环通过 gridview 它将只读取 2 行。它正在跳过最后一行。

有什么想法吗

4

1 回答 1

0

我解决了这个问题。在 RowCreated 事件上编写此代码。我在 RowDataBound 上编写了这段代码,因此创建的标题行不一致。

protected void gvCustomers_RowCreated(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.Header) { GridViewRow newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

TableCell cell1 = new TableHeaderCell();
cell1.ColumnSpan = 1; //e.Row.Cells.Count;
cell1.Text = "Expected";

TableCell cell2 = new TableCell();
cell2.ColumnSpan = 2;
cell2.Text = "One";

TableCell cell3 = new TableCell();
cell3.ColumnSpan = 2;
cell3.Text = "Two";

TableCell cell4 = new TableCell();
cell4.ColumnSpan = 2;
cell4.Text = "Three";

newHeaderRow.Cells.Add(cell1);
newHeaderRow.Cells.Add(cell2);
newHeaderRow.Cells.Add(cell3);
newHeaderRow.Cells.Add(cell4);

((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);

}

于 2013-02-27T10:44:33.107 回答