iam looking for a method to set the color specific only for 1 cell by clicking on it.
I build a Canteen Voter, which you can choice between 4 menues each day.
My DataGridView is connected to a database from which it receives all information and when you click on a cell it is also stored in the database for the logged-in user.
Now I would like to be able to color the cells individually when clicking.
So far I only found the SelectedCells property, but this actually only sets a color for "SelectedCells" when these cells lose focus again they are white again.
I would have liked them to be able to keep this color to make it even clearer to the user which dish he had just chosen.
CodePudding user response:
You should set DataGridViewCell.Style
property.
Take a look at this example
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.RowIndex == dataGridView1.NewRowIndex)
{
return;
}
var dataGridViewCellStyle =
new DataGridViewCellStyle(dataGridView1.DefaultCellStyle)
{
BackColor = Color.Gold
};
dataGridView1[e.ColumnIndex, e.RowIndex].Style = dataGridViewCellStyle;
}