繁体   English   中英

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

[英]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 ,其中有SelectionBackColorSelectionForeColor属性。

DataGridView 使用样式继承思想,以防您发现您选择的样式没有被应用:

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

利用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;
}

这是您可以复制和粘贴的简单且有效的版本:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    (sender as DataGridView).CurrentRow.DefaultCellStyle.SelectionBackColor = Color.Green;
}

也可以在“DataGridView”的设计器中更改颜色

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM