How to Change the Color in One Cell of DataGridView

Save

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)

    {

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

    }

    }

    }

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

References

  • Photo Credit Ablestock.com/AbleStock.com/Getty Images
Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

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

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