0
votes

I've modified my GridView to have an extra Header row, however that extra row has caused my grid view row count to be incorrect.

Basically, when I want to save the Gridview now, it doesn't recognize the last item line. In my current test I have 5 Item Lines, however only 4 of them are being saved.

My code for creating the additional header Line:

  protected void gvStatusReport_RowDataBound(object sender, GridViewRowEventArgs e)
   {
      // This grid has multiple rows, fake the top row.
      if (e.Row.RowType == DataControlRowType.Header)
      {
         SortedList FormatCells = new SortedList();
         FormatCells.Add("1", ",6,1");
         FormatCells.Add("2", "Time Spent,7,1");
         FormatCells.Add("3", "Total,2,1");
         FormatCells.Add("4", ",6,1");

         GetMultiRowHeader(e, FormatCells);
      }
    }

The function to create a new row:

       public void GetMultiRowHeader(GridViewRowEventArgs e, SortedList GetCels)
   {
      GridViewRow row;
      IDictionaryEnumerator enumCels = GetCels.GetEnumerator();

      row = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
      while (enumCels.MoveNext())
      {
         string[] count = enumCels.Value.ToString().Split(Convert.ToChar(","));
         TableCell cell = new TableCell();
         cell.RowSpan = Convert.ToInt16(count[2].ToString());
         cell.ColumnSpan = Convert.ToInt16(count[1].ToString());
         cell.Controls.Add(new LiteralControl(count[0].ToString()));
         cell.HorizontalAlign = HorizontalAlign.Center;
         row.Cells.Add(cell);
      }

      e.Row.Parent.Controls.AddAt(0, row);
   }

Then when I'm going to save, I loop through the rows:

 int totalRows = gvStatusReport.Rows.Count;

      for (int rowNumber = 0; rowNumber < totalRows; rowNumber++)
      {

However the first line doesn't seem to have any of the columns that the item row has, and the last line doesn't even show up.

My problem is that I do need the extra header line, but what is the best way to fix this?

2

2 Answers

0
votes

I recall working with this and i think you cannot add to GridView.Controls like this

you have to get GridView.Controls[0] as Table (works only after data bind, but that should be ok in your case) and then add to the table row collection Table.Rows.Add(myrow)

0
votes

I've managed to solve this issue with the following code:

protected void gvStatusReport_RowCreated(object sender, GridViewRowEventArgs e)
   {
      if (e.Row.RowType == DataControlRowType.Header)
      {
         GridView HeaderGrid = (GridView)sender;
         GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
         TableCell HeaderCell = new TableCell();
         HeaderCell.ColumnSpan = 6;
         HeaderGridRow.Cells.Add(HeaderCell);

         HeaderCell = new TableCell();
         HeaderCell.Text = "Time Spent";
         HeaderCell.ColumnSpan = 7;
         HeaderGridRow.Cells.Add(HeaderCell);

         HeaderCell = new TableCell();
         HeaderCell.Text = "Total";
         HeaderCell.ColumnSpan = 2;
         HeaderGridRow.Cells.Add(HeaderCell);

         HeaderCell = new TableCell();
         HeaderCell.ColumnSpan = 6;
         HeaderGridRow.Cells.Add(HeaderCell);

         gvStatusReport.Controls[0].Controls.AddAt(0, HeaderGridRow);
      }
   }