Add Grouping Header in GridView


protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

DataRowView drv = (DataRowView)e.Row.DataItem;

GridView _gv1 = (GridView)sender;

Table gvTable = (Table)e.Row.Parent;

 

if (drv["aDate"].ToString() != strGroup)

{

if (gvTable != null)

{

TableCell tc = new TableCell();

tc.Text = String.Format("{0:d}", drv["aDate"]);

tc.Height = Unit.Pixel(20);//you can change this to adjust to the space you want

tc.Attributes[

"Align"] = "Left";

tc.Font.Bold = true;

tc.Attributes[

"ColSpan"] = _gv1.Columns.Count.ToString(); ;

 

GridViewRow gr = new GridViewRow(0, 0, DataControlRowType.Separator, DataControlRowState.Normal);

gr.Cells.Add(tc);

gvTable.Controls.AddAt(_gv1.Controls[0].Controls.Count – 1, gr);

}

strGroup = drv[

"aDate"].ToString();

t += 1;

}

else

{

}

e.Row.Cells[0].Text =

" ";

if (t % 2 == 0)

{

e.Row.BackColor = System.Drawing.

Color.Azure;

}

else

{

e.Row.BackColor = System.Drawing.

Color.GhostWhite;

}

 

}

}

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s