How to Change the Color in One Cell of DataGridView


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 color. 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 color in one particular cell in DataGridView control.

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

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

  • 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 backBrush, System.Drawing.Brush

    foreBrush, bool alignToRight)



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


    foreBrush = new SolidBrush(Color.White);




    catch(Exception ex)


    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);




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

Related Searches


  • Photo Credit Images
Promoted By Zergnet


You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!