如何更改datagridview选定的行背景颜色?

如何在C#windows应用程序中更改datagridview选定的行背景颜色?

在DataGridView上有一个DefaultCellStyle ,里面有SelectionBackColorSelectionForeColor属性。

DataGridView使用样式inheritance的想法,以防您发现未选择的样式:

http://msdn.microsoft.com/en-us/library/1yef90x0.aspx

来吧男人…必须有一个简单的解决方案,最后得到一个。

 dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Blue; dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red; 

这对我有用,没有复杂的代码,没有事件处理。 我以前做过,但无法回想起这么想发布它会帮助别人和我将来:)

利用DataGridViewCell的事件CellEnterCellLeave您可以尝试这样的事情:

 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); } 

这是我的代码

 private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e) { dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.Maroon; dataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.White; }