GridView - A very powerful control that provides an easy interface to add, update, delete, and
display data.
Sometimes we may need to manipulate the text, cells, and rows to fit our needs.
GridView is great for very simple tables. Sometimes we need to specify the format of the data, and the way it is rendered in the table, more exactly.
DataList could be used but doesn't have all the features such as paging and sorting that are commonly required.
If you need to use the GridView, but also need more control on the way that the table is rendered, you can use the GridView events and properties.
RowDataBound - The most common event used. This event is fired every time a row is bound to data. Whenever this event is fired, we will have access to the current row and all of its data, so we can manipulate the table, row, cells, and or controls of the table accordingly.
Other important events are the DataBound and the Load event.
Load event - fires when the GridView is loaded and has not been attached to any data yet. In this event the user can set properties such as the color of the border, themes, or any other rendering options that are not dependent on the data itself.
DataBound - is similar to the RowDataBound in that both are fired after a bound event has happened. The difference is that DataBound is fired once after the entire Grid has been bound; while the RowDataBound is fired every time a row is bound, meaning it will almost always be fired more than once. So you can use the DataBound to manipulate the table based on the data contained in it.
Thanks to http://www.simple-talk.com/dotnet/asp.net/take-row-level-control-of-your-gridview/ Visit the link for a full breakdown.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView
OnRowDataBound="gvComorbidities_RowDataBound"
DataKeyNames="comorbidityID"
runat="server"
ID="gvComorbidities"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<p>Date Reported</p>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td><a href='<%# String.Format("Comorbidities.aspx?comorbidityID={0}&Patient_ID={1}&OperationID={2}",DataBinder.Eval(Container.DataItem,"comorbidityid"),DataBinder.Eval(Container.DataItem,"patient_id"),DataBinder.Eval(Container.DataItem,"operationid")) %>'><%# DataBinder.Eval(Container.DataItem, "dateoffirstsymptoms", "{0:d}")%></a></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gvComorbidities_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Look for data items
if (e.Row.RowType == DataControlRowType.DataRow)
{
string comorbidityid = gvComorbidities.DataKeys[e.Row.DataItemIndex].Value.ToString();
string qscomorbidityid = Request.QueryString["comorbidityid"].ToString();
if (comorbidityid == qscomorbidityid)
{
e.Row.BackColor = System.Drawing.Color.LightGray;
}
}
}