将标题行添加到gridview获胜';不允许保存最后一行项目
本文关键字:最后 保存 项目 一行 不允许 添加 标题 gridview 获胜 | 更新日期: 2023-09-27 18:20:38
我修改了GridView,使其具有一个额外的Header行,但是该额外的行导致我的网格视图行数不正确。
基本上,当我现在想保存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++)
{
然而,第一行似乎没有项目行所具有的任何列,最后一行甚至没有显示。
我的问题是我确实需要额外的标题行,但解决这个问题的最佳方法是什么?
我记得我处理过这个问题,我认为你不能像这个那样添加到GridView.Controls
您必须获得GridView.Controls[0] as Table
(仅在数据绑定后有效,但在您的情况下应该可以),然后将其添加到表行集合Table.Rows.Add(myrow)
我已经用以下代码解决了这个问题:
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);
}
}