Click here to Skip to main content
Licence 
First Posted 21 Apr 2003
Views 244,804
Bookmarked 79 times

Conditional Formatting ASP.NET DataGrid Columns

By | 21 Apr 2003 | Article
How to format ASP.NET datagrid columns by handling ItemDataBound event at run time.
Prize winner in Competition "ASP.NET Mar 2003"
Sample Image - GridColumnFormatting.gif

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

About the Author

Softomatix

Web Developer

United States United States

Member

To learn more about us, Please visit us at http://www.netomatix.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey19:26 7 Feb '12  
Generalbackground image Pinmembermarko jovanovic2:14 7 Apr '06  
i've tried to set background image for cell based on some condition with css
style but couldn't set cssclass attribute of the cell, is there way to do this
vb code:
e.item.cells(1).cssclass = "cellimg"
 
style.css:
.cell
{
background-image:url(images/img.gif);
}
what i do wrong?
 
roopyu
QuestionIs there any equivalent situation for WinForms? PinsussAnonymous2:41 14 Jun '05  
Generalmulti Datagrid Pinsusstenzeel.1:36 24 Jul '04  
GeneralHi PinsussAnonymous18:58 12 Apr '04  
GeneralDataGrid formating and Editing data in DataGrid Pinmemberbinmak3:06 21 Feb '04  
QuestionHow this method gets called ? PinmemberInnovatus8:07 13 Feb '04  
AnswerRe: How this method gets called ? Pinmemberthomashungnguyen3:08 4 Nov '05  
GeneralInvalidCastException Pinmemberlewinpj2:40 27 Nov '03  
GeneralChanging a Cells color based on another cells value Pinmembercdwaddell2:36 20 Aug '03  
QuestionHow to create a Scrollable DataGrid with a Fixed Header? Pinsussanonymous1:22 14 Jun '03  
AnswerRe: How to create a Scrollable DataGrid with a Fixed Header? PinmemberSoftomatix3:57 17 Jun '03  
AnswerRe: How to create a Scrollable DataGrid with a Fixed Header? PinsussAnonymous9:05 17 Jul '03  
GeneralRe: How to create a Scrollable DataGrid with a Fixed Header? PinmemberHJvdWijk21:28 6 Jan '04  
GeneralRe: How to create a Scrollable DataGrid with a Fixed Header? Pinmemberreyabreu8:58 29 Oct '04  
AnswerRe: How to create a Scrollable DataGrid with a Fixed Header? PinsussAnonymous3:00 28 Oct '04  
QuestionHow to get width of columns for bounded datagrid Pinmembernitts4:06 12 Jun '03  
AnswerRe: How to get width of columns for bounded datagrid PinmemberSoftomatix12:20 12 Jun '03  
GeneralFormatting actual cell data PinmemberKerry Sanders18:07 5 Jun '03  
GeneralRe: Formatting actual cell data PinmemberSoftomatix12:21 12 Jun '03  
GeneralRe: Formatting actual cell data PinmemberSoftomatix12:22 12 Jun '03  
Generalthe color stayed in the same cell after sorted Pinmemberdpthinh20:48 21 Apr '03  
GeneralRe: the color stayed in the same cell after sorted PinmemberSoftomatix3:16 22 Apr '03  

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.

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