Hi everyone!
I am trying to make a table with a header something like this:
+---------------------------+
| | | Day |
| Task | Name |-------------|
| | |0|1|2|3|4|5|6|
+------+------+-+-+-+-+-+-+-+
| | | | | | | | | |
I am trying to do it with GridView, because I need to be able to edit these lines, etc. After several hours I figured out how to add a new header row into the GridView, and set the Task and Name column's cell rowspan to achieve the above results.
Inserting a new header row, and adding dynamic number of additional cells to each row (since I don't know how many days will be there design-time)
protected void BacklogGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
int cellcount = e.Row.Cells.Count;
int days = items[0].Estimates.Count - 1; //items is the business object
GridView gv = sender as GridView;
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i <= cellcount - 2; ++i)
{
e.Row.Cells[i].RowSpan = 2;
}
e.Row.Cells[cellcount-1].ColumnSpan = days + 1;
if (gv.HasControls())
{
GridViewRow tr = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
for (short i = 0; i <= days; ++i)
{
TableCell td = new TableCell();
td.Text = i.ToString();
tr.Cells.Add(td);
}
((Table)gv.Controls[0]).Rows.Add(tr);
}
}
else if (e.Row.RowIndex != -1)
{
e.Row.Cells[cellcount - 1].Text = items[e.Row.RowIndex].Estimates[0].ToString();;
if (l!=null) l.Text = items[e.Row.RowIndex].Estimates[0].ToString();
for (short i = 1; i <= days; ++i)
{
TableCell td = new TableCell();
td.Text = items[e.Row.RowIndex].Estimates[i].ToString();
e.Row.Cells.Add(td);
}
}
}
However I am facing some new problems with this. First of all, when I click on a commandfield's edit button, and accept the changes, the rowupdating event won't fire, instead, it jumps to the next row, as if I had clicked the edit there.
Secondly, since I dont know how many days will I have in design time, I have to use the above method to add new cells to each row, corresponding to the number of days from the data source. This leads to a situation, where I have no control over these cells, since they are just dumb table cells, and cannot be edited in same way as they would be BoundFields.
Any ideas?