Rapunzo
Rapunzo

Reputation: 986

Changing cell Backcolor on DataGridView CellMouseClick

I want to invert cell back color by clicking on cell and using below code

 private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor =
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor == Color.Black
                ? Color.White
                : Color.Black;
    }

but this code works on cell leave. I want to paint cell instantly on click. Which event should I use?

Upvotes: 2

Views: 539

Answers (1)

TaW
TaW

Reputation: 54453

You are mistaken. The code works immediately. You just can't see it as the click is also selecting the Cell and the selection color is taking precedence.

To make it show right away simply add this to the event:

dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = false;

Upvotes: 1

Related Questions