1

I have a method that I call on page load that iterates through the rows in a gridview. What I would like to happen is for a header row to be inserted when the 'SectionID' of the row changes (it is a data key.)

There will be 6 additional header rows inserted and my code is doing just that except it is inserting all of the additional rows at the top (under the original header and before the first data row.) I want it to insert when the 'SectionID' changes.

Here is my method:

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);
            }
        }
    }
}
Matthew
  • 9,851
  • 4
  • 46
  • 77
arcaderob
  • 481
  • 1
  • 7
  • 21

0 Answers0