Click here to Skip to main content
15,445,641 members
Articles / Programming Languages / C#
Article
Posted 28 Apr 2002

Stats

392.5K views
7.1K downloads
73 bookmarked

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
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

 
GeneralAlignment Pin
Johan Hakkesteegt29-May-08 1:21
MemberJohan Hakkesteegt29-May-08 1:21 
QuestionChanging color of datagrid row depending upon column values Pin
rizwan127-Mar-08 1:35
Memberrizwan127-Mar-08 1:35 
AnswerRe: Changing color of datagrid row depending upon column values Pin
Peter Felgate29-May-08 1:01
MemberPeter Felgate29-May-08 1:01 
QuestionHow to fill color for one cell in datagrid with vb.net??? Pin
Member 437798911-Mar-08 15:45
MemberMember 437798911-Mar-08 15:45 
Generalcolor from row Pin
chehreghany21-Sep-07 21:51
Memberchehreghany21-Sep-07 21:51 
GeneralThank you for this code Pin
davidbowlby27-Apr-07 10:08
Memberdavidbowlby27-Apr-07 10:08 
GeneralChanging the keyboard Pin
zazx12-Jul-06 22:32
Memberzazx12-Jul-06 22:32 
Generalhighlighting only the selected text portion of cell Pin
AnurRS13-Jun-06 0:33
MemberAnurRS13-Jun-06 0:33 
GeneralType is not defined error Pin
Totoo1-Jun-06 3:46
MemberTotoo1-Jun-06 3:46 
QuestionValue from another cell ? Pin
durtal24-Nov-05 10:16
Memberdurtal24-Nov-05 10:16 
AnswerRe: Value from another cell ? Pin
Jacobus Meintjes27-Dec-05 0:13
MemberJacobus Meintjes27-Dec-05 0:13 
Generalcolor an array of rows Pin
xrado4-Jul-05 1:33
Memberxrado4-Jul-05 1:33 
General.NET Compact Framework Pin
Member 16904875-Apr-05 15:16
MemberMember 16904875-Apr-05 15:16 
GeneralRe: .NET Compact Framework Pin
mp123418-Jan-07 0:57
Membermp123418-Jan-07 0:57 
GeneralRe: .NET Compact Framework Pin
websjwans19-Mar-07 22:39
Memberwebsjwans19-Mar-07 22:39 
GeneralRe: .NET Compact Framework Pin
hayles29-Aug-07 22:34
Memberhayles29-Aug-07 22:34 
GeneralAdding the row with various number of columns than the rest Pin
Member 16780499-Mar-05 2:33
MemberMember 16780499-Mar-05 2:33 
QuestionHow to change value Pin
mtnhan28-Jan-05 17:28
Membermtnhan28-Jan-05 17:28 
AnswerRe: How to change value Pin
Mazdak29-Jan-05 22:36
MemberMazdak29-Jan-05 22:36 
GeneralRe: How to change value Pin
mtnhan30-Jan-05 14:00
Membermtnhan30-Jan-05 14:00 
GeneralHighlight all cells in a column Pin
tullm11-Nov-04 4:44
Membertullm11-Nov-04 4:44 
Generalwin form datagrid Pin
Pradeep K V7-Sep-04 2:55
MemberPradeep K V7-Sep-04 2:55 
GeneralA little abt rows Pin
Sameer Khan11-Mar-03 7:30
MemberSameer Khan11-Mar-03 7:30 
GeneralRe: A little abt rows Pin
Mazdak11-Mar-03 8:35
MemberMazdak11-Mar-03 8:35 
GeneralRe: A little abt rows Pin
Sameer Khan12-Mar-03 12:16
MemberSameer Khan12-Mar-03 12:16 

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.