How do I change the datagridview selected row background color in C# windows applications?
30
votes
6 Answers
43
votes
Come on man... there has to be a simple solution, and finally got one.
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Blue;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red;
This worked for me, no complex codes, no event handling. I did it before but was not able to recall so thought posting it would help others and me in future :)
33
votes
3
votes
Taking advantage of DataGridViewCell
's events CellEnter
and CellLeave
you might try something like this:
private void foobarDataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCellStyle fooCellStyle = new DataGridViewCellStyle();
fooCellStyle.BackColor = System.Drawing.Color.LightYellow;
this.VariableFinderDataGridView.CurrentCell.Style.ApplyStyle(fooCellStyle);
}
private void foobarFinderDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCellStyle barCellStyle = new DataGridViewCellStyle();
barCellStyle.BackColor = System.Drawing.Color.White;
this.VariableFinderDataGridView.CurrentCell.Style.ApplyStyle(barCellStyle);
}
1
votes
1
votes