Changing datagridview cell color dynamically

156,167

Solution 1

This works for me

dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red;

Solution 2

Implement your own extension of DataGridViewTextBoxCell and override Paint method like this:

class MyDataGridViewTextBoxCell : DataGridViewTextBoxCell
{
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
        DataGridViewElementStates cellState, object value, object formattedValue, string errorText,
        DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        if (value != null)
        {
            if ((bool) value)
            {
                cellStyle.BackColor = Color.LightGreen;
            }
            else
            {
                cellStyle.BackColor = Color.OrangeRed;
            }
        }
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
            formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}

}

Then in the code set CellTemplate property of your column to instance of your class

columns.Add(new DataGridViewTextBoxColumn() {CellTemplate = new MyDataGridViewTextBoxCell()});

Solution 3

Thanks it working

here i am done with this by qty field is zero means it shown that cells are in red color

        int count = 0;

        foreach (DataGridViewRow row in ItemDg.Rows)
        {
            int qtyEntered = Convert.ToInt16(row.Cells[1].Value);
            if (qtyEntered <= 0)
            {
                ItemDg[0, count].Style.BackColor = Color.Red;//to color the row
                ItemDg[1, count].Style.BackColor = Color.Red;

                ItemDg[0, count].ReadOnly = true;//qty should not be enter for 0 inventory                       
            }
            ItemDg[0, count].Value = "0";//assign a default value to quantity enter
            count++;
        }

    }

Solution 4

If you want every cell in the grid to have the same background color, you can just do this:

dataGridView1.DefaultCellStyle.BackColor = Color.Green;
Share:
156,167
fifamaniac04
Author by

fifamaniac04

Updated on November 04, 2021

Comments

  • fifamaniac04
    fifamaniac04 over 2 years

    I have a dataGridView object that is populated with data. I want to click a button and have it change the color of the background of the cell. This is what I currently have

    foreach(DataGridViewRow row in dataGridView1.Rows)
    {
        foreach(DataGridViewColumn col in dataGridView1.Columns)
        {
                //row.Cells[col.Index].Style.BackColor = Color.Green; //doesn't work
                //col.Cells[row.Index].Style.BackColor = Color.Green; //doesn't work
            dataGridView1[col.Index, row.Index].Style.BackColor = Color.Green; //doesn't work
        }
    } 
    

    ALL of these three cause the table to be redrawn over itself in an overlapping manner and trying to re-size the tables becomes a mess. when clicking on a cell, the value remains highlighted and the backcolor doesn't change.

    Q: How can I change the backcolor of an individual cell after the table exists?

  • Ehsan
    Ehsan about 8 years
    @t4thilina, Glad it helped. Cheers :)
  • Robert H.
    Robert H. almost 7 years
    If you attempt this in the form's constructor, it does not work. It's working for me in the OnLoad override, though.
  • n00dles
    n00dles almost 7 years
    lol, I was choosing the best colours for 'warning' and 'ok' earlier, and I also chose Color.OrangeRed for 'warning' in the end, but Color.SpringGreen for 'OK'.
  • n00dles
    n00dles almost 7 years
    I had to call dataGridView1.Refresh() after to get colour to change. That reset colours on rows sort got me searching now...
  • rémy
    rémy about 6 years
    You can also set the column's CellTemplate via this.YourColumn.CellTemplate or this.dataGridView.Columns["YourName"].CellTemplate