Formatting Grid View - Change appearance of rows
Thursday, February 28, 2008 5:36:57 PM

In DataGrid and GridView controls there is a property that lets you specify specify style for alternate rows. For DataGrid this property is AlternatingItemStyle and for GridView it is AlternatingRowStyle. For one of my reports the requirement was slightly different. The client wanted to alter behavior of every third row in the view. Thats where GirdView's RowCreated event came in very handy. You simply subscribe to this event and your event handler gets called for every row creation. In this event you can check the properties of Row getting created and you can alter the rendering property. Following code shows how I was able to set the background color of every third row in GridView.
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (((e.Row.RowIndex+1) % 3) == 0)
{
e.Row.BackColor = System.Drawing.Color.Snow;
}
}
}






