Emad's Blog

Thoughts/Tips/Shortcuts/Ideas from a .NET software engineer

Archive for September, 2007

ASP.NET datagrid conditional formatting

Posted by emadmagdy on September 15, 2007

The idea to conditionally formatting cells in an ASP.NET data grid lies simply with the ItemDataBound Event of the datagrid.

 

Example:

protected void gridData_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Customer cust = (Customer)e.Item.DataItem;

        Int32 daysRemaining =cust.SubscriptionDaysRemaining;
        if (daysRemaining <= 0)
        {
            e.Item.Cells[12].ForeColor = Color.Red;
            e.Item.Cells[12].Font.Bold = true;
            e.Item.Cells[12].Font.Italic = true;
            e.Item.Cells[12].Font.Size = new FontUnit(16);
        } 

    }

Posted in Uncategorized | Leave a Comment »