Search code examples
c#datagridviewcell

Change datagridview cell color by clicking in c#(winform)



I have a datagrid on the form.
and i want to that,
when i click on any cell on any row
the cell back color can change to red color for example.
how can i do that...


Solution

  • use cell click event

    in the event just assign cell.backcolor to color.red

    private void GridView_CellClick(object sender, DataGridViewCellEventArgs e)

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