Click here to Skip to main content
11,707,470 members (80,643 online)
Click here to Skip to main content

Change datagrid cell properties

, 2 Aug 2006 69.6K 625 21
Rate this:
Please Sign up or sign in to vote.
Change the color of a datagrid cell

Sample Image - datagridcellproperties.jpg

I've searched the internet for examples of datagrids and how to change one or more cell properties to another background or font color. A lot of people in forums said it isn't possible, but finally it seems quite simple. Combining different articles and examples gave me the solution I wanted.

Introduction

This article describes how to create a datagrid and change the color of one or more cells based upon one or more conditions. This article contains very simple code of ASP.NET.

Using the code

What we want is a datagrid with employees. Employees with a salary less than 5000 must be displayed in red. We use a XML-file to display in the datagrid. 

First we create a datagrid on our aspx page.

<asp:DataGrid id="dgEmployees" runat="server" AutoGenerateColumns="False" OnItemDataBound="dgBoundItems">
    <AlternatingItemStyle CssClass="datagridAltItem">

    <ItemStyle CssClass="datagridItem">

    <HeaderStyle CssClass="datagridHeader">

</asp:DataGrid>

Then we load the XML-file into the dataset in the pageload. Then we bound the columns into the datagrid in de code behind page, so we can set some properties.

public DataSet ds = new DataSet(); 

private void Page_Load(object sender, System.EventArgs e) 
{
    ///Read the XML-file into the dataset 
    ds.ReadXml(MapPath("employees.xml")); 
    DataView dv1 = new DataView(); 
    dv1.Table = ds.Tables[0]; 
    ///Bind the columns into the datagrid
    BoundColumn datagridcol; 
    datagridcol = new BoundColumn(); 
    datagridcol.HeaderText = "EmpID"; 
    datagridcol.HeaderStyle.Width = Unit.Pixel(100); 
    datagridcol.ItemStyle.Width = Unit.Pixel(100); 
    datagridcol.DataField = "ID"; 
    dgEmployees.Columns.Add(datagridcol); 
    datagridcol = new BoundColumn(); 
    datagridcol.HeaderText = "Name"; 
    datagridcol.HeaderStyle.Width = Unit.Pixel(200); 
    datagridcol.ItemStyle.Width = Unit.Pixel(200); 
    datagridcol.DataField = "Name"; 
    dgEmployees.Columns.Add(datagridcol); 
    datagridcol = new BoundColumn(); 
    datagridcol.HeaderText = "Salary"; 
    datagridcol.HeaderStyle.Width = Unit.Pixel(200); 
    datagridcol.ItemStyle.Width = Unit.Pixel(200); 
    datagridcol.DataField = "Salary"; 
    dgEmployees.Columns.Add(datagridcol); 
    dgEmployees.DataSource = dv1; 
    dgEmployees.DataBind(); 
    dgEmployees.Visible = true; 
}

So now the datgrid is filled with the information from the XML-File. Now we want the salaries displayed in red when they are less than 5000. We can do this in the OnItemDataBound. When binding the datagrid the procedure dgBoundItems is called. Here we can check every cell of the table that will be created. With GetCellIndex we get the columnnumber of the table. We only have to check the third column, so GetCellIndex must be equal to 2. The DataSetIndex gives the rownumber. Rownumber 0 is the header. We want to start after the header, so DataSetIndex must be greater than 0. Finally we check if the salary is less than 5000. We change the cell fore-color into red.

///C#

public void dgBoundItems(object sender, DataGridItemEventArgs e) 
{ 
    foreach(TableCell cell in e.Item.Cells) 
    { 
        ///e.Item.DataSetIndex = row 
        ///e.Item.Cells.GetCellIndex(cell) = column 
        if (e.Item.Cells.GetCellIndex(cell) == 2 && e.Item.DataSetIndex > 0) 
        {
            ///If the salary is less than 5000 the color must be red. 
            if (Convert.ToInt32(cell.Text.Trim()) < 5000) 
            { 
                cell.ForeColor = Color.Red; 
            } 
        } 
    } 
}

Conclusion

A simple way to change the text-color in a cell. With this example in mind it is possible to change a lot of properties of a single cell. Background-color, mouse-over actions, etc.

Hope my example helps a lot of other people.

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

Richard van Schie
Web Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

 
Generalchange row color based on the datagrid cell value Pin
suki_y4-Feb-09 10:14
membersuki_y4-Feb-09 10:14 
Generaldatagrid in asp.net one cell value are change other all rows are changes use clinet site . Pin
dilipbangar6-Dec-08 2:23
memberdilipbangar6-Dec-08 2:23 
QuestionGetting the Same reaults from a GridView (ASP.2.0) Pin
Shane Horn19-Nov-07 1:38
memberShane Horn19-Nov-07 1:38 

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.150819.1 | Last Updated 3 Aug 2006
Article Copyright 2006 by Richard van Schie
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid