我有一个方法,我在页面加载时调用,遍历gridview中的行。我想要发生的是当行的“SectionID”发生变化时插入标题行(它是一个数据键。)
将插入6个额外的标题行,我的代码正在执行此操作,除非它在顶部插入所有其他行(在原始标题下和第一个数据行之前。)我希望它在插入时插入'SectionID'发生了变化。
这是我的方法:
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);
}
}
}
}