【问题标题】:C# Winforms: Customize DataGridView selected gridcolorC# Winforms:自定义 DataGridView 选定的网格颜色
【发布时间】:2014-08-08 11:10:43
【问题描述】:

我有DataGridView,它有 3 列和 3 行。如果用户选择一行,我想让该行的网格改变颜色。我对 C# 完全陌生,我不知道如何实现我的目标。请帮帮我。谢谢

【问题讨论】:

  • 伙计们,我不需要更改单元格的正面或背面颜色。我需要更改网格线(边框)的颜色。无论如何感谢您的尝试:)

标签: c# winforms datagridview colors grid


【解决方案1】:

我了解到您询问的是关于修改单元格 GridLines 的外观并单独进行修改。

根据MSDN,这可能的。然而,这似乎需要付出很大的努力。您需要继承 DataGridview 并广泛修改示例代码以动态工作。玩了一段时间后,我决定,这不值得。我建议采用其他方法之一来标记选择。 (或者获得大量声望并悬赏它..)

【讨论】:

  • 我同意,这不值得。我想这是一种非正统的事情,谢谢你的回答;)
【解决方案2】:

查看DataGridView 类的属性,您可以选择在DataGridView.GridColor 属性中设置网格颜色。

获取或设置分隔 DataGridView 单元格的网格线的颜色。

下面的例子:

dataGridView1.GridColor = SystemColors.ActiveBorder;

【讨论】:

  • 谢谢,但这不是我想要的。我希望 GridColor 改变,而不是 BackColor。
【解决方案3】:

您可以使用 DataGridView.GridColor 属性对其进行更改。这将改变网格线的颜色。 查看链接-DataGridView.GridColor

 dataGridView1.GridColor = SystemColors.Blue;

这将改变网格中的每一行。您能否指定要更改的确切行的内容?我想你只想要行的行。如果您只想更改行线颜色,则可以使用以下代码:

dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Red;
dataGridView1.Rows[0].DefaultCellStyle.ForeColor = Color.White;

您可以在下面看到它在该行的 CellClick 事件中使用:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dgv = sender as DataGridView;
    if (dgv == null)
        return;
    if (dgv.CurrentRow.Selected)
    {
        dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Red;
        dataGridView1.Rows[0].DefaultCellStyle.ForeColor = Color.White;
    }
}

希望对你有所帮助。

【讨论】:

  • 我想隐藏所选行周围的水平线,并且我想使该行的垂直线改变颜色。这在Winforms 中是否可行?
  • 如果你想隐藏这些行使用这个代码 - dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None。但我不认为你只能改变水平。
【解决方案4】:
DataGridViewRow dgRow = dataGridView1.Rows[e.RowIndex];
dgRow.DefaultCellStyle.BackColor = Color.Red; 
dgRow.DefaultCellStyle.ForeColor = Color.Yellow;

【讨论】:

  • 请在您的代码 sn-p 中添加解释和上下文。
猜你喜欢
  • 2018-01-24
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 2013-03-11
  • 2018-07-20
  • 1970-01-01
相关资源
最近更新 更多