Tuesday, August 19, 2008

Gridview Data display header and detail in same column

Gridview Data display header and detail - ASP.NET Forums


private string _categoryName = String.Empty;
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;
if (gvr.RowType == DataControlRowType.DataRow)
{
DataRowView drv = gvr.DataItem as DataRowView;
if (drv == null) { return; }
string categoryName = drv["GENREPARENT"].ToString();
if (!categoryName.Equals(_categoryName))
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.HorizontalAlign = HorizontalAlign.Center;
cell.Text = categoryName;
row.Cells.Add(cell);
gvProducts.Controls[0].Controls.AddAt(gvProducts.Controls[0].Controls.Count - 1, row);
_categoryName = categoryName;
}
}
}

No comments: