Click here to Skip to main content
11,478,873 members (64,009 online)
Click here to Skip to main content

Changing the background color of cells in a DataGrid

, 28 Apr 2002 309.5K 5.8K 70
Rate this:
Please Sign up or sign in to vote.
How to change the background color of cells in a DataGrid depending on their value
<!-- Download Links --> <!-- Article image -->

Sample Image - Custom_DataGridColumnStyl.jpg

<!-- Add the rest of your HTML here -->

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:

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:

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:

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

Share

About the Author

Mazdak
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

 
GeneralAlignment Pin
Johan Hakkesteegt29-May-08 2:21
memberJohan Hakkesteegt29-May-08 2:21 
QuestionChanging color of datagrid row depending upon column values Pin
rizwan127-Mar-08 2:35
memberrizwan127-Mar-08 2:35 
AnswerRe: Changing color of datagrid row depending upon column values Pin
Peter Felgate29-May-08 2:01
memberPeter Felgate29-May-08 2:01 
QuestionHow to fill color for one cell in datagrid with vb.net??? Pin
Member 437798911-Mar-08 16:45
memberMember 437798911-Mar-08 16:45 
Generalcolor from row Pin
chehreghany21-Sep-07 22:51
memberchehreghany21-Sep-07 22:51 
GeneralThank you for this code Pin
davidbowlby27-Apr-07 11:08
memberdavidbowlby27-Apr-07 11:08 
GeneralChanging the keyboard Pin
zazx12-Jul-06 23:32
memberzazx12-Jul-06 23:32 
Generalhighlighting only the selected text portion of cell Pin
AnurRS13-Jun-06 1:33
memberAnurRS13-Jun-06 1:33 
GeneralType is not defined error Pin
Totoo1-Jun-06 4:46
memberTotoo1-Jun-06 4:46 
QuestionValue from another cell ? Pin
durtal24-Nov-05 11:16
memberdurtal24-Nov-05 11:16 
AnswerRe: Value from another cell ? Pin
Ice Dragon27-Dec-05 1:13
memberIce Dragon27-Dec-05 1:13 
Generalcolor an array of rows Pin
xrado4-Jul-05 2:33
memberxrado4-Jul-05 2:33 
General.NET Compact Framework Pin
Jonathan Dudley5-Apr-05 16:16
memberJonathan Dudley5-Apr-05 16:16 
GeneralRe: .NET Compact Framework Pin
mp123418-Jan-07 1:57
membermp123418-Jan-07 1:57 
GeneralRe: .NET Compact Framework Pin
websjwans19-Mar-07 23:39
memberwebsjwans19-Mar-07 23:39 
GeneralRe: .NET Compact Framework Pin
hayles29-Aug-07 23:34
memberhayles29-Aug-07 23:34 
GeneralAdding the row with various number of columns than the rest Pin
Ivan Milutinovic9-Mar-05 3:33
memberIvan Milutinovic9-Mar-05 3:33 
QuestionHow to change value Pin
mtnhan28-Jan-05 18:28
membermtnhan28-Jan-05 18:28 
AnswerRe: How to change value Pin
Mazdak29-Jan-05 23:36
memberMazdak29-Jan-05 23:36 
GeneralRe: How to change value Pin
mtnhan30-Jan-05 15:00
membermtnhan30-Jan-05 15:00 
GeneralHighlight all cells in a column Pin
tullm11-Nov-04 5:44
membertullm11-Nov-04 5:44 
Generalwin form datagrid Pin
Pradeep K V7-Sep-04 3:55
memberPradeep K V7-Sep-04 3:55 
GeneralA little abt rows Pin
Sameer Khan11-Mar-03 8:30
memberSameer Khan11-Mar-03 8:30 
GeneralRe: A little abt rows Pin
Mazdak11-Mar-03 9:35
memberMazdak11-Mar-03 9:35 
GeneralRe: A little abt rows Pin
Sameer Khan12-Mar-03 13:16
memberSameer Khan12-Mar-03 13:16 
GeneralRe: A little abt rows Pin
Mazdak14-Mar-03 9:56
memberMazdak14-Mar-03 9:56 
GeneralRe: A little abt rows Pin
Mazdak14-Mar-03 9:59
memberMazdak14-Mar-03 9:59 
GeneralRe: A little abt rows Pin
Sameer Khan29-Apr-03 8:39
memberSameer Khan29-Apr-03 8:39 
GeneralRe: A little abt rows Pin
Mazdak29-Apr-03 21:09
memberMazdak29-Apr-03 21:09 
Generalon a similar note Pin
Sameer Khan10-Mar-03 12:51
memberSameer Khan10-Mar-03 12:51 
GeneralRe: on a similar note Pin
Sameer Khan10-Mar-03 14:09
memberSameer Khan10-Mar-03 14:09 
GeneralRe: on a similar note Pin
Sameer Khan12-Mar-03 11:22
memberSameer Khan12-Mar-03 11:22 
QuestionHow to change the color of text, not the background color, please help Pin
jasonli16-Sep-02 10:00
sussjasonli16-Sep-02 10:00 
AnswerRe: How to change the color of text, not the background color, please help Pin
Mazdak17-Sep-02 10:06
memberMazdak17-Sep-02 10:06 
AnswerRe: How to change the color of text, not the background color, please help Pin
albean22-Jul-03 18:21
memberalbean22-Jul-03 18:21 
GeneralSane example Pin
Paul Watson29-Jul-02 6:56
editorPaul Watson29-Jul-02 6:56 
GeneralRe: Sane example Pin
Mazdak31-Jul-02 10:05
memberMazdak31-Jul-02 10:05 
GeneralLink Broken!!!!!! Pin
Mazdak29-Apr-02 3:13
memberMazdak29-Apr-02 3:13 
GeneralRe: Link Broken!!!!!! Pin
Nish [BusterBoy]29-Apr-02 15:05
memberNish [BusterBoy]29-Apr-02 15:05 
GeneralRe: Link Broken!!!!!! Pin
Mazdak30-Apr-02 6:32
memberMazdak30-Apr-02 6:32 
GeneralRe: Link Broken!!!!!! Pin
Chris Maunder29-Apr-02 19:06
adminChris Maunder29-Apr-02 19:06 
GeneralRe: Link Broken!!!!!! Pin
Mazdak30-Apr-02 4:51
memberMazdak30-Apr-02 4:51 
GeneralRe: Link Broken!!!!!! Pin
Nish [BusterBoy]30-Apr-02 7:08
memberNish [BusterBoy]30-Apr-02 7:08 
GeneralRe: Link Broken!!!!!! Pin
Mazdak30-Apr-02 9:39
memberMazdak30-Apr-02 9:39 
GeneralRe: Link Broken!!!!!! Pin
Nish [BusterBoy]30-Apr-02 15:00
memberNish [BusterBoy]30-Apr-02 15:00 
GeneralRe: Link Broken!!!!!! Pin
Mazdak30-Apr-02 23:07
memberMazdak30-Apr-02 23:07 

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
Web01 | 2.8.150520.1 | Last Updated 29 Apr 2002
Article Copyright 2002 by Mazdak
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid