0

我已将 GridView 修改为有一个额外的标题行,但是该额外行导致我的网格视图行数不正确。

基本上,当我现在想保存 Gridview 时,它无法识别最后一个项目行。在我当前的测试中,我有 5 个项目行,但是其中只有 4 个被保存。

我用于创建附加标题行的代码:

  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);
      }
    }

创建新行的函数:

       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);
   }

然后,当我要保存时,我遍历行:

 int totalRows = gvStatusReport.Rows.Count;

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

但是,第一行似乎没有项目行所具有的任何列,最后一行甚至没有出现。

我的问题是我确实需要额外的标题行,但解决此问题的最佳方法是什么?

4

2 回答 2

0

我记得和这个一起工作,我认为你不能GridView.Controls像这样添加

您必须获取GridView.Controls[0] as Table(仅在数据绑定后有效,但在您的情况下应该没问题),然后添加到表行集合中Table.Rows.Add(myrow)

于 2013-07-02T11:02:03.087 回答
0

我已经设法使用以下代码解决了这个问题:

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);
      }
   }
于 2013-07-03T10:29:16.610 回答