Click here to Skip to main content
Licence 
First Posted 28 Apr 2002
Views 267,195
Bookmarked 68 times

Changing the background color of cells in a DataGrid

By | 28 Apr 2002 | Article
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:

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

About the Author

Mazdak

Web Developer

Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAlignment PinmemberJohan Hakkesteegt1:21 29 May '08  
QuestionChanging color of datagrid row depending upon column values Pinmemberrizwan11:35 27 Mar '08  
AnswerRe: Changing color of datagrid row depending upon column values PinmemberPeter Felgate1:01 29 May '08  
QuestionHow to fill color for one cell in datagrid with vb.net??? PinmemberMember 437798915:45 11 Mar '08  
Generalcolor from row Pinmemberchehreghany21:51 21 Sep '07  
GeneralThank you for this code Pinmemberdavidbowlby10:08 27 Apr '07  
GeneralChanging the keyboard Pinmemberzazx22:32 12 Jul '06  
Generalhighlighting only the selected text portion of cell PinmemberAnurRS0:33 13 Jun '06  
GeneralType is not defined error PinmemberTotoo3:46 1 Jun '06  
QuestionValue from another cell ? Pinmemberdurtal10:16 24 Nov '05  
AnswerRe: Value from another cell ? PinmemberIce Dragon0:13 27 Dec '05  
Generalcolor an array of rows Pinmemberxrado1:33 4 Jul '05  
General.NET Compact Framework PinmemberJonathan Dudley15:16 5 Apr '05  
GeneralRe: .NET Compact Framework Pinmembermp12340:57 18 Jan '07  
GeneralRe: .NET Compact Framework Pinmemberwebsjwans22:39 19 Mar '07  
GeneralRe: .NET Compact Framework Pinmemberhayles22:34 29 Aug '07  
GeneralAdding the row with various number of columns than the rest PinmemberIvan Milutinovic2:33 9 Mar '05  
QuestionHow to change value Pinmembermtnhan17:28 28 Jan '05  
AnswerRe: How to change value PinmemberMazdak22:36 29 Jan '05  
GeneralRe: How to change value Pinmembermtnhan14:00 30 Jan '05  
GeneralHighlight all cells in a column Pinmembertullm4:44 11 Nov '04  
Generalwin form datagrid PinmemberPradeep K V2:55 7 Sep '04  
GeneralA little abt rows PinmemberSameer Khan7:30 11 Mar '03  
GeneralRe: A little abt rows PinmemberMazdak8:35 11 Mar '03  
GeneralRe: A little abt rows PinmemberSameer Khan12:16 12 Mar '03  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 29 Apr 2002
Article Copyright 2002 by Mazdak
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid