Reputation: 1007
My gridview is similar to this:
12- CategoryA - other columns 13- CategoryA - other columns 14- CategoryA - other columns 15- CategoryA - other columns 16- CategoryB - other columns 17- CategoryB - other columns 18- CategoryB - other columns
What I want is smthing like that:
CategoryA (colspan 2)
12 - other columns
13 - other columns
14 - other columns
15 - other columns
Category B (colspan 2)
16 - other columns
17 - other columns
18 - other columns
Can I do this by modifing the datatable which I bind as datasource ? Or is there an easier way ?
Upvotes: 0
Views: 1223
Reputation: 1967
I believe RowDataBound event is what you'll need. You can keep track of what previous category was shown and what category will be shown.
class MyClass
{
private string CurrentCategory{ get; set; }
// Load_Page with databinding the GridView{ }
protected void mygridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow &&
(e.Row.DataItem as DataRowView)["mycolumn"].ToString() != CurrentCategory))
{
GridViewRow tr = new GridViewRow(e.Row.RowIndex +1, e.Row.RowIndex + 1,
DataControlRowType.DataRow, e.Row.RowState);
TableCell newTableCell = new TableCell();
newTableCell.Text = (e.Row.DataItem as DataRowView)["mycolumn"].ToString();
CurrentCategory = (e.Row.DataItem as DataRowView)["mycolumn"].ToString();
tr.Cells.Add(newTableCell);
((Table)e.Row.Parent).Rows.Add(tr);
}
}
}
Code is provided as-is and not tested.
Upvotes: 2