Ivan Ivan - 3 months ago 13
C# Question

Datagridview background color for all cells

I want to set background color for all cells when i click on one.
Sorry am new in winforms

Like on picture

enter image description here

Like on row 6! All cells has red color on click.

I also try with

DefaultCellStyle
and
DefaultRowStyle
but that is again only for one cell.

Does maybe i need to loop between cells and put for each bg color on select ?

Cab someone give me answer?

Answer

have you tried this?

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
        CellStyle.BackColor = Color.Red;
        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = CellStyle;
    }

or if you want to change background color of complete row then try this

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red; 
    }