How to Change the Color in One Cell of DataGridView

Written by alexander gokhfeld
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Change the Color in One Cell of DataGridView
DataGridView control can change the visual style of particular table cells. (Ablestock.com/AbleStock.com/Getty Images)

The DataGridView control in the Microsoft .NET Framework allows users to edit visual attributes of table elements, such as column, rows and cells. Each cell within the DataGridView control can have its own text font, background and foreground colour. The rows, columns and groups of cells share particular style characteristics by default. You need to override the Paint property of the DataGrid class to change the colour in one particular cell in DataGridView control.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Click the "Start" button in Windows and select the "Microsoft Visual Studio."

  2. 2

    Click "Open" and select the C# program in which you want to change the colour in one particular cell in DataGridView control.

  3. 3

    Add the following code to the DataGridView control after the "public void DataGrid ()" line in your program:

    public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn

     {
    
          protected override void Paint(System.Drawing.Graphics g,
    
               System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager
    
               source, int rowNum, System.Drawing.Brush back Brush, System.Drawing.Brush
    
               fore Brush, bool alignToRight)
    
          {
    
               OneCell{
    
                    object o = this.GetColumnValueAtRow(source, rowNum);
    
                    if( o!= null)
    
                    {
    
                         char c = ((string)o)[0];
    
                         if( c > 'F')
    
                         {
    
                              backBrush = new LinearGradientBrush(bounds,
    
                                   Color.FromArgb(255, 200, 200),
    
                                   Color.FromArgb(128, 20, 20),
    
                                   LinearGradientMode.BackwardDiagonal);
    
                              foreBrush = new SolidBrush(Color.White);
    
                         }
    
                    }
    
               }
    
                catch(Exception ex)
    
               ChangeColor{
    
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
    
               }
    
          }
    
     }
    
  4. 4

    Click "File" and "Save" to change the colour in the "o" cell in the DataGridView control of your program.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.