How To Show Some Rows Differently in a GridView
Suppose you're showing a list of customers, and you want to highlight any customer who'd been with you since before 1920. Maybe you also want the JoinedOn field to show the number of months for customers who've been with you for less than a year. It's fairly easy.######Add OnRowDataBound Handler to GridView
The easiest place to change the data is just after the data gets bound to the row. So:
<asp:GridView runat="server" ID="CustomerGrid" OnRowDataBound="CustomerGrid_OnRowDataBound">
</asp:GridView>
Make the changes in CustomerGrid_OnRowDataBound
protected void CustomerGrid_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
// Make sure we aren't in header/footer rows
if (row.DataItem == null)
{
return;
}
Customer customer = row.DataItem as Customer;
if (/* Do your conditionals here*/)
{
// Change Row formatting here OR
// Add new cells
// Or create an entirely different cells
}
}