Click here to Skip to main content
11,926,961 members (49,503 online)
Click here to Skip to main content
Add your own
alternative version


45 bookmarked

Practical use of the DataGrid's ItemDataBound event.

, 27 Oct 2004
Rate this:
Please Sign up or sign in to vote.
Using the ItemDataBound event to modify the content and formatting of any cell in a DataGrid.

Sample Image


Because of its simplicity, flexibility and many possible uses, the DataGrid class is one of the most well known and used classes of the .NET framework. The use of the DataGrid's ItemDataBound event introduces a whole new world of possibilities, letting you conditionally modify displayed data, format, as well as insert some client side code for any cell in your DataGrid. The use of this event can sometimes be a smart alternative to adding calculated columns to a dataset.

The source code available for this tutorial is a working example of what you can do with the ItemDataBound event; a DataGrid displays customer information, a balance value is displayed in red if its value is negative. For the sake of this tutorial, we'll pretend that Jamaica has invaded the North American continent, so North American countries are displayed as "Jamaica" with the insertion of client side code to create a hyperlink and map this link's onClick event to a JavaScript function (popup window). The source code contains an Access database and two web forms; the DataGrid and a form letting the user view complete details of the customer in the popup window.


The ItemDataBound event happens once for every row, after the data is bound to the DataGrid and before the page is rendered to the client.

Using the code

// First we add our handler (OnItemDataBound) to the datagrid's ItemDataBound event
this.myDataGrid.ItemDataBound += new 
// This is our handler for the datagrid's ItemDataBound event.
private void OnItemDataBound(object sender, 
        System.Web.UI.WebControls.DataGridItemEventArgs e)
    // Check if the current row contains items; if it's
    // a header or footer row that will throw an error
    if (e.Item.ItemType == ListItemType.Item || 
        e.Item.ItemType == ListItemType.AlternatingItem)
        // Get a copy of the "dgLabel2" label that contains the 
        // "Balance" value
        Label lblBalance = (Label)e.Item.FindControl("dgLabel2");
        // Convert the value contained in the label to a double type
        double dblBalance = Convert.ToDouble(lblBalance.Text);
        // If that numeric value is negative
        if(dblBalance < 0)
            // ...display it in red
            e.Item.Cells[3].ForeColor = System.Drawing.Color.Red;
        // Get a copy of the "dgLabel1" label that contains the 
        // "Country" value
        Label lblCountry = (Label)e.Item.FindControl("dgLabel1");
        // ...convert it to a string
        string strCountry = lblCountry.Text;
        // If it's a North American country...
        if(strCountry == "USA" || 
            strCountry == "Mexico" || 
            strCountry == "Canada")
            // Get a copy of the "dgLabel0" label that contains the 
            // "CustomerID" value, we'll use it in the query string 
            // for the popup
            Label lblID = (Label)e.Item.FindControl("dgLabel0");
            // ...convert it to a string
            string strID = lblID.Text;
            // Replace the "Country" value displayed in the datagrid
            // with "Jamaica", placed in a hyperlink who's OnClick 
            // event calls a javascript "popup" window
            e.Item.Cells[2].Text = "<a href=\"popup.aspx?id=" + strID +
                "\" onClick=\"popup(this.href); return false;\">Jamaica</a>";

Points of Interest

The ItemCreated event is a close sibling of ItemDataBound. This event happens before ItemDataBound, when the row is created but does not yet contain any data, so you can't use it to read or modify data.


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

Luc Archambault
Web Developer
Canada Canada
I have been working with different web technologies (ASP, ASP.NET, ColdFusion, Perl/CGI) for the past 15 years. I live in Montreal, where I work for a little consultant group specializing in Microsoft technologies.

You may also be interested in...

Comments and Discussions

QuestionHow to get Balance value on dgLabel2 ? Pin
Member 115775252-Apr-15 19:37
memberMember 115775252-Apr-15 19:37 
GeneralMy vote of 3 Pin
babu saravanan11-Apr-13 3:59
memberbabu saravanan11-Apr-13 3:59 
GeneralRe: My vote of 3 Pin
ProgramFOX17-Apr-13 8:15
member ProgramFOX17-Apr-13 8:15 
GeneralRe: My vote of 3 Pin
babu saravanan17-Apr-13 8:34
memberbabu saravanan17-Apr-13 8:34 
GeneralRe: My vote of 3 Pin
ProgramFOX17-Apr-13 8:35
member ProgramFOX17-Apr-13 8:35 
GeneralRe: My vote of 3 Pin
babu saravanan17-Apr-13 8:38
memberbabu saravanan17-Apr-13 8:38 
Generaldate wise mail send Pin
papubiswal14-Mar-11 0:35
memberpapubiswal14-Mar-11 0:35 
GeneralExcellent Pin
NaumanLeghari29-Dec-06 1:17
memberNaumanLeghari29-Dec-06 1:17 
GeneralSimple Solution Pin
muffingal20-Jun-06 10:51
membermuffingal20-Jun-06 10:51 
GeneralYou don't need labels [modified] Pin
Harteex8-Jun-06 6:02
memberHarteex8-Jun-06 6:02 
GeneralThank you Pin
gungk31-Mar-06 9:27
membergungk31-Mar-06 9:27 
QuestionKidding me? Pin
eanderson3-Nov-04 12:52
membereanderson3-Nov-04 12:52 
AnswerRe: Kidding me? Pin
Anonymous21-Dec-04 12:47
sussAnonymous21-Dec-04 12:47 
GeneralRe: Kidding me? Pin
Anonymous3-Feb-05 9:22
sussAnonymous3-Feb-05 9:22 
GeneralRe: Kidding me? Pin
robdeyric14-Aug-09 7:39
memberrobdeyric14-Aug-09 7:39 
AnswerRe: Kidding me? Pin
TechnicaOne3-Feb-05 15:15
memberTechnicaOne3-Feb-05 15:15 
AnswerRe: Kidding me? Pin
Anonymous8-Jun-05 16:23
sussAnonymous8-Jun-05 16:23 
AnswerRe: Kidding me? Pin
shakoosh18-Jul-05 10:51
membershakoosh18-Jul-05 10:51 
AnswerRe: Kidding me? Pin
Christian Islas27-Jul-09 7:54
memberChristian Islas27-Jul-09 7:54 
AnswerRe: Kidding me? Pin
Adam Tolley14-Jul-10 10:09
memberAdam Tolley14-Jul-10 10:09 
AnswerRe: Kidding me? Pin
johnmcpherson1023-Jul-12 5:48
groupjohnmcpherson1023-Jul-12 5:48 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 27 Oct 2004
Article Copyright 2004 by Luc Archambault
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid