I am writing a C# application (windows forms) in which I have a 10x10 DataGridView which represents a maze. When a cell is clicked, I add the corresponding x and y to a 2D array. Each cell that is clicked, should display a black background.
On CellClick:
int row = dataGridView1.CurrentCell.RowIndex;
int column = dataGridView1.CurrentCell.ColumnIndex;
maze[row, column] = 1;
dataGridView1.Refresh();
I've also implemented a handler for CellFormatting event:
if (maze[e.RowIndex,e.ColumnIndex] == 1){
e.CellStyle.BackColor = Color.Black;
}
Now when I click a cell, the style is not being updated. When I click another cell after that, the previous cell's style is updated. I've tried to both Refresh()
and Update
the control, but no luck.
How can I solve this problem, so a cell's style is immediately updated when it is clicked?