shariq_khan
shariq_khan

Reputation: 681

Change the datagridview color specified with column

I am working on winform C# datagirdview and I would like to change the color of a particular row in my datagridview. The row should be changed to red when the value of columncell is false.

Upvotes: 0

Views: 818

Answers (2)

spajce
spajce

Reputation: 7092

Use the CellFormatting Event

   private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex == 1)
        {
            if (e.Value != null)
            {
                if ((bool)e.Value)
                    dataGridView2.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
                else
                    dataGridView2.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
            }
        }
    }

EDIT:

The value of e.value is base on the e.ColumnIndex for more Details

Upvotes: 2

dutzu
dutzu

Reputation: 3920

I don't know if by means of some sort of DataBinding like in WPF you can achieve this, but another way would be to hook to the event that is triggered when a row is created and change the color there.

Try the RowAdded event.

Upvotes: 1

Related Questions