Click here to Skip to main content
11,577,522 members (23,564 online)
Click here to Skip to main content

Conditional Formatting ASP.NET DataGrid Columns

, 21 Apr 2003 264.5K 80
Rate this:
Please Sign up or sign in to vote.
How to format ASP.NET datagrid columns by handling ItemDataBound event at run time.
Sample Image - GridColumnFormatting.gif
<!-- Add the rest of your HTML here -->

Like our previous article, How to remove DataGrid column, this article will try to answer another most frequently asked question about DataGrid controls, how can I conditionally format columns of a data grid control at run time? Believe it or not the answer to this question is very straight forward. When you call DataBind on a DataGrid control or for that matter whenever a grid gets bound to its datasource, ASP.Net framework fires ItemDataBound event. You can add an event handler for this event to take control of the rendering of DataGrid control.

The key to the solution is capturing ItemDataBound event. When the page recieves this event, complete information about the data row is available in DataGridItemEventArgs parameter of the event handler. Item property of this parameter contains information about the table rows and cells that will get rendered on the page. This parameter also contains the information about the DataItem that is being used to render information for the row. The data item corresponds to DataRowView object for that row's data. You can get the value for any given data field based on the field name or zero based index of data column.

For this article we have used the sample Northwind database and bound it to our DataGrid column.

private void OnNWDataBound(object sender, <BR>             System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView rv = (DataRowView)e.Item.DataItem;
        // Get fourth column value.
        Int32 nUnitsInStock = Convert.ToInt32(rv.Row.ItemArray[4]);
        if (nUnitsInStock < 20)
        {
           e.Item.Cells[4].BackColor = Color.Red;
        }
    }
}
                        

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Softomatix
Web Developer
United States United States
To learn more about us, Please visit us at http://www.netomatix.com

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
manoj kumar choubey7-Feb-12 19:26
membermanoj kumar choubey7-Feb-12 19:26 
Nice
Generalbackground image Pin
marko jovanovic7-Apr-06 2:14
membermarko jovanovic7-Apr-06 2:14 
QuestionIs there any equivalent situation for WinForms? Pin
Anonymous14-Jun-05 2:41
sussAnonymous14-Jun-05 2:41 
Generalmulti Datagrid Pin
tenzeel.24-Jul-04 1:36
susstenzeel.24-Jul-04 1:36 
GeneralHi Pin
Anonymous12-Apr-04 18:58
sussAnonymous12-Apr-04 18:58 
GeneralDataGrid formating and Editing data in DataGrid Pin
binmak21-Feb-04 3:06
memberbinmak21-Feb-04 3:06 
QuestionHow this method gets called ? Pin
Innovatus13-Feb-04 8:07
memberInnovatus13-Feb-04 8:07 
AnswerRe: How this method gets called ? Pin
thomashungnguyen4-Nov-05 3:08
memberthomashungnguyen4-Nov-05 3:08 
GeneralInvalidCastException Pin
lewinpj27-Nov-03 2:40
memberlewinpj27-Nov-03 2:40 
GeneralChanging a Cells color based on another cells value Pin
cdwaddell20-Aug-03 2:36
membercdwaddell20-Aug-03 2:36 
QuestionHow to create a Scrollable DataGrid with a Fixed Header? Pin
anonymous14-Jun-03 1:22
sussanonymous14-Jun-03 1:22 
AnswerRe: How to create a Scrollable DataGrid with a Fixed Header? Pin
Softomatix17-Jun-03 3:57
memberSoftomatix17-Jun-03 3:57 
AnswerRe: How to create a Scrollable DataGrid with a Fixed Header? Pin
Anonymous17-Jul-03 9:05
sussAnonymous17-Jul-03 9:05 
GeneralRe: How to create a Scrollable DataGrid with a Fixed Header? Pin
HJvdWijk6-Jan-04 21:28
memberHJvdWijk6-Jan-04 21:28 
GeneralRe: How to create a Scrollable DataGrid with a Fixed Header? Pin
reyabreu29-Oct-04 8:58
memberreyabreu29-Oct-04 8:58 
AnswerRe: How to create a Scrollable DataGrid with a Fixed Header? Pin
Anonymous28-Oct-04 3:00
sussAnonymous28-Oct-04 3:00 
QuestionHow to get width of columns for bounded datagrid Pin
nitts12-Jun-03 4:06
membernitts12-Jun-03 4:06 
AnswerRe: How to get width of columns for bounded datagrid Pin
Softomatix12-Jun-03 12:20
memberSoftomatix12-Jun-03 12:20 
GeneralFormatting actual cell data Pin
Kerry Sanders5-Jun-03 18:07
memberKerry Sanders5-Jun-03 18:07 
GeneralRe: Formatting actual cell data Pin
Softomatix12-Jun-03 12:21
memberSoftomatix12-Jun-03 12:21 
GeneralRe: Formatting actual cell data Pin
Softomatix12-Jun-03 12:22
memberSoftomatix12-Jun-03 12:22 
Generalthe color stayed in the same cell after sorted Pin
dpthinh21-Apr-03 20:48
memberdpthinh21-Apr-03 20:48 
GeneralRe: the color stayed in the same cell after sorted Pin
Softomatix22-Apr-03 3:16
memberSoftomatix22-Apr-03 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 22 Apr 2003
Article Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid