动态地在每行之后重复标题行

如何在gridview的每一行之后重复标题行?

您可以将代码添加到网格的rowdatabound-event:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { GridViewRow row = new GridViewRow(e.Row.RowIndex, 0, DataControlRowType.Header, DataControlRowState.Normal); TableCell cell = new TableCell(); cell.Controls.Add(new Label { Text = "Header" }); //as needed row.Cells.Add(cell); Table tbl = (e.Row.Parent as Table); tbl.Controls.AddAt(e.Row.RowIndex * 2 + 1, row); } } 

在这个例子中,当你在代码中编写自己的代码时,我已经完全删除了gridview头。

你不能不重写控制渲染方法。 如何使用转发器并将其放入项目分隔元素中?

好吧,在项目模板中也可以更容易地为标题添加标记。 为这样的事情重写Render方法可能有点过头了。

但如果有理由这样做,至少在你的问题中遗漏了。

如果您需要在每个项目行中添加标题行标记,为什么不将标题中当前包含的标记包含在ItemTemplate中?