Ivan Kochurkin
Ivan Kochurkin

Reputation: 4501

Update style for all cells from DataGridView

Is it possible to update style for all cells from DataGridView without iteration over them like example below?

for (int i = 0; i < dgv.Columns.Count; i++)
    for (int j = 0; j < dgv.Rows.Count; j++)
        if (dgv[i, j].Style != style)
            dgv[i, j].Style = style;

My question is an actual due to slow speed of slyle updating for all cells.

Upvotes: 1

Views: 833

Answers (2)

King King
King King

Reputation: 63377

If you want to apply the same style to all the cells, simply use the DefaultCellStyle of the datagridview.

dataGridView.DefaultCellStyle.BackColor = Color.Green;

The answer of Killercam would be helpful when you want to apply different styles to different cells on the same rows.

Upvotes: 1

MoonKnight
MoonKnight

Reputation: 23831

You can do this on a row-by-row basis:

foreach (DataGridViewRow row in dataGridView.Rows)
    Row.DefaultCellStyle.BackColor = Color.Red;

or

for (int r = 0; r < dataGridView.Rows.Count; r++)
    dataGridView.Rows[r].DefaultCellStyle.BackColor = Color.Red;

where using the DefaultCellStyle you can set other properties as well.

I hope this helps.

Upvotes: 0

Related Questions