1

我有一个在页面加载时调用的方法,它遍历网格视图中的行。我想要发生的是在行的“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);
            }
        }
    }
}
4

0 回答 0