adding header row to gridview won't allow you to save the last item row
Posted
by
Lex
on Stack Overflow
See other posts from Stack Overflow
or by Lex
Published on 2013-07-02T10:42:28Z
Indexed on
2013/07/02
11:05 UTC
Read the original article
Hit count: 366
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?
© Stack Overflow or respective owner