Click here to Skip to main content
15,892,643 members
Articles / Programming Languages / C#
Article

Changing the background color of cells in a DataGrid

Rate me:
Please Sign up or sign in to vote.
4.81/5 (33 votes)
28 Apr 2002 402.2K   7.2K   73   46
How to change the background color of cells in a DataGrid depending on their value

Sample Image - Custom_DataGridColumnStyl.jpg

In this article I want to show how you can change the background color of a specific cells in a datagrid. .To do this you first have to create class derived from DataGridTextBoxColumn or DataGridColumnStyle. You then have to override the Paint() method of these classes. Note that this method has three overloaded versions. 

In this method first you can check the value of cell in the column with the GetColumnValueAtRow method then set the brush you want, fill the cell rectangle then write the text with your proper font. Here is the sample code I used:

C#
protected override void Paint(Graphics g, Rectangle Bounds, CurrencyManager Source,
                              int RowNum, Brush BackBrush, Brush ForeBrush, 
                              bool AlignToRight) 
{
    
    bool bdel = (bool) GetColumnValueAtRow(Source, RowNum);

    if(bdel == true)
        BackBrush = Brushes.Coral;
    else
        BackBrush = Brushes.White;

    g.FillRectangle(BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);

    System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif, 
                                        (float)8.25 );
    g.DrawString( bdel.ToString(), font, Brushes.Black, Bounds.X, Bounds.Y);
}

Here I use column with a bool value and change the colors for true cells. Here you can change the font for them very easily too. 

You can also change the overall style of the datagrid using the DataGridTableStyle and DataGridColumnStyle classes. The function below demonstrates this:

C#
private void CreateDataGridStyle() 
{
    DataGridColumnStyle GridDelColumn;
    DataGridColumnStyle GridSeqStyle;
    DGStyle = new DataGridTableStyle();  //DGStyle is DataGridTableStyle

    DGStyle.MappingName = "Table1";

    GridSeqStyle = new DataGridTextBoxColumn();
    GridSeqStyle.MappingName = "Column1";
    GridSeqStyle.HeaderText = "Column1";
    GridSeqStyle.Width = 100;
    DGStyle.GridColumnStyles.Add(GridSeqStyle);

    PropertyDescriptorCollection pcol = this.BindingContext[myDataSet, 
                                             "Table1"].GetItemProperties();

    GridDelColumn = new ColumnStyle(pcol["Table1"]);
    GridDelColumn.MappingName = "Column2";
    GridDelColumn.HeaderText = "Column2";
    GridDelColumn.Width = 100;
    DGStyle.GridColumnStyles.Add(GridDelColumn);

    DGStyle.AllowSorting         = true;
    DGStyle.RowHeadersVisible    = true;
}

And after that you  add the styles to datagrid:

C#
CreateDataGridStyle();
myDataGrid.TableStyles.Add(DGStyle);

myDataGrid.SetDataBinding(myDataSet,"Table1");

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
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: How to change the color of text, not the background color, please help Pin
albean22-Jul-03 17:21
albean22-Jul-03 17:21 
GeneralSane example Pin
Paul Watson29-Jul-02 5:56
sitebuilderPaul Watson29-Jul-02 5:56 
GeneralRe: Sane example Pin
Mazdak31-Jul-02 9:05
Mazdak31-Jul-02 9:05 
GeneralLink Broken!!!!!! Pin
Mazdak29-Apr-02 2:13
Mazdak29-Apr-02 2:13 
GeneralRe: Link Broken!!!!!! Pin
Nish Nishant29-Apr-02 14:05
sitebuilderNish Nishant29-Apr-02 14:05 
GeneralRe: Link Broken!!!!!! Pin
Mazdak30-Apr-02 5:32
Mazdak30-Apr-02 5:32 
GeneralRe: Link Broken!!!!!! Pin
Chris Maunder29-Apr-02 18:06
cofounderChris Maunder29-Apr-02 18:06 
GeneralRe: Link Broken!!!!!! Pin
Mazdak30-Apr-02 3:51
Mazdak30-Apr-02 3:51 
Thank you Chris,But still download link does not work,should I post the rest of article to submit@codeproject.com ????Cry | :(( Cry | :((

Mazy

"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd

GeneralRe: Link Broken!!!!!! Pin
Nish Nishant30-Apr-02 6:08
sitebuilderNish Nishant30-Apr-02 6:08 
GeneralRe: Link Broken!!!!!! Pin
Mazdak30-Apr-02 8:39
Mazdak30-Apr-02 8:39 
GeneralRe: Link Broken!!!!!! Pin
Nish Nishant30-Apr-02 14:00
sitebuilderNish Nishant30-Apr-02 14:00 
GeneralRe: Link Broken!!!!!! Pin
Mazdak30-Apr-02 22:07
Mazdak30-Apr-02 22:07 

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.