Click here to Skip to main content
15,887,027 members
Articles / Web Development / ASP.NET
Article

Change datagrid cell properties

Rate me:
Please Sign up or sign in to vote.
3.69/5 (8 votes)
2 Aug 20062 min read 83.9K   692   21   3
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.

C#
<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.

C#
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#
///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


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalchange row color based on the datagrid cell value Pin
suki_y4-Feb-09 10:14
suki_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
dilipbangar6-Dec-08 2:23 
QuestionGetting the Same reaults from a GridView (ASP.2.0) Pin
Hornwood50919-Nov-07 1:38
Hornwood50919-Nov-07 1:38 

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.