Antonio Mailtraq
Antonio Mailtraq

Reputation: 1407

Background color in GridView

In my GridView I have the column name Switch which can contain in each row, three different values; L, B and T.

You can associate a different background color according to values L, B and T ?

For example when the row contain in column name Switch the value L the background color is yellow, when the value B the background color on the row is green and the value T the background color on the row is red.

Can you help me?

Upvotes: 2

Views: 191

Answers (2)

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

You can try this in RowsAdded Event

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        var row = dataGridView1.Rows[e.RowIndex];
        if (String.CompareOrdinal(row.Cells["Switch"].Value.ToString(), "L") == 0)
        {
            row.DefaultCellStyle.BackColor = Color.Yellow;
        }
        else if (String.CompareOrdinal(row.Cells["Switch"].Value.ToString(), "B") == 0)
        {
            row.DefaultCellStyle.BackColor = Color.Blue;
        }
    }

Upvotes: 1

Vanest
Vanest

Reputation: 916

You can do something like this, if the grid is present in a windows form,

dataGridView1.Rows.OfType<DataGridViewRow>()
                  .Where(x => Convert.ToString(x.Cells["Switch"].Value) == "L").ToList()
                  .ForEach(x => x.DefaultCellStyle.BackColor = Color.Yellow);

Hope this helps...

Upvotes: 1

Related Questions