C# 如何使用自定义方法将标题行插入Gridview
我有一个在页面加载时调用的方法,该方法迭代gridview中的行。我希望在行的“SectionID”更改时插入标题行(它是一个数据键) 将插入6个额外的标题行,我的代码就是这样做的,除了在顶部插入所有额外的行(在原始标题下,在第一个数据行之前)。我希望它在“SectionID”更改时插入 以下是我的方法:C# 如何使用自定义方法将标题行插入Gridview,c#,asp.net,gridview,header-row,C#,Asp.net,Gridview,Header Row,我有一个在页面加载时调用的方法,该方法迭代gridview中的行。我希望在行的“SectionID”更改时插入标题行(它是一个数据键) 将插入6个额外的标题行,我的代码就是这样做的,除了在顶部插入所有额外的行(在原始标题下,在第一个数据行之前)。我希望它在“SectionID”更改时插入 以下是我的方法: protected void addHeaders() { foreach (GridViewRow row in GridView1.Rows) { if (row.
protected void addHeaders() {
foreach (GridViewRow row in GridView1.Rows) {
if (row.RowType == DataControlRowType.DataRow) {
tmpSectionID = GridView1.DataKeys[row.RowIndex].Values["SectionID"].ToString();
System.Diagnostics.Debug.WriteLine(tmpSectionID);
if (sectionID != tmpSectionID) {
sectionID = tmpSectionID;
GridView gvw = (GridView)GridView1;
DataRowView drv = (DataRowView)row.DataItem;
GridViewRow HeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
Table tbl = row.Parent as Table;
Table myTable = (Table)GridView1.Controls[0];
TableCell HeaderCell = new TableCell();
HeaderCell.ColumnSpan = this.GridView1.Columns.Count;
HeaderCell.Style.Add("font-weight", "bold");
HeaderCell.Style.Add("background-color", "#22539B");
HeaderCell.Style.Add("color", "white");
HtmlGenericControl HeaderSpan = new HtmlGenericControl("span");
HeaderCell.ToolTip = "Tooltip Here";
HeaderSpan.InnerHtml = "Inserted Row";
HeaderCell.Controls.Add(HeaderSpan);
HeaderRow.Cells.Add(HeaderCell);
myTable.Rows.AddAt(1, HeaderRow);
}
}
}
}