[英]How do I change the datagridview selected row background color?
如何在 C# windows 应用程序中更改 datagridview 选定的行背景颜色?
来吧伙计......必须有一个简单的解决方案,最后得到了一个。
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Blue;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red;
这对我有用,没有复杂的代码,没有事件处理。 我以前做过,但无法回忆,所以认为发布它会在未来帮助他人和我:)
在 DataGridView 上有一个DefaultCellStyle
,其中有SelectionBackColor
和SelectionForeColor
属性。
DataGridView 使用样式继承思想,以防您发现您选择的样式没有被应用:
利用DataGridViewCell
的事件CellEnter
和CellLeave
你可以尝试这样的事情:
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;
}
这是您可以复制和粘贴的简单且有效的版本:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
(sender as DataGridView).CurrentRow.DefaultCellStyle.SelectionBackColor = Color.Green;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.