Click here to Skip to main content
13,290,261 members (52,804 online)
Click here to Skip to main content
Add your own
alternative version


80 bookmarked
Posted 21 Apr 2003

Conditional Formatting ASP.NET DataGrid Columns

, 21 Apr 2003
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;


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


About the Author

Web Developer
United States United States
To learn more about us, Please visit us at

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey7-Feb-12 20:26
membermanoj kumar choubey7-Feb-12 20:26 
Generalbackground image Pin
marko jovanovic7-Apr-06 3:14
membermarko jovanovic7-Apr-06 3:14 
QuestionIs there any equivalent situation for WinForms? Pin
Anonymous14-Jun-05 3:41
sussAnonymous14-Jun-05 3:41 
Generalmulti Datagrid Pin
tenzeel.24-Jul-04 2:36
susstenzeel.24-Jul-04 2:36 
GeneralHi Pin
Anonymous12-Apr-04 19:58
sussAnonymous12-Apr-04 19:58 
GeneralDataGrid formating and Editing data in DataGrid Pin
binmak21-Feb-04 4:06
memberbinmak21-Feb-04 4:06 
QuestionHow this method gets called ? Pin
Innovatus13-Feb-04 9:07
memberInnovatus13-Feb-04 9:07 
AnswerRe: How this method gets called ? Pin
thomashungnguyen4-Nov-05 4:08
memberthomashungnguyen4-Nov-05 4:08 
GeneralInvalidCastException Pin
lewinpj27-Nov-03 3:40
memberlewinpj27-Nov-03 3:40 
GeneralChanging a Cells color based on another cells value Pin
cdwaddell20-Aug-03 3:36
membercdwaddell20-Aug-03 3:36 
We want to change the color of a previous cell based on the value in the current cell. For example, say we are ranking items as they are read into the datagrid. We have several columns of data we want to rank for first and second place. Once we encounter a value that is greater than the previous high value we would like to change the previous high values cell color to yellow and the new high cells color to green. For example column 1 in the following table would have 12 highlighted in green and 10 highlighted in yellow. See the following table. Column 2 would have 11 highlighted green and the two 10 highlighted yellow. and finally column 3 would have 14 green and 10 yellow.

User Name 1 2 3 Total
PLayer1 9 10 14 33
Player2 12 10 7 29
Player3 7 11 10 28
Player4 10 7 6 23

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 22 Apr 2003
Article Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid