更改 DataGridView 单元格中按钮的颜色

问题描述 投票:0回答:8

我有一个大型 DataGridView 控件,其中有几个单元格,其中大部分包含一个按钮。 我怎样才能改变这些按钮的颜色?

这会更改按钮的“轮廓”,但不会更改按钮本身。

row.Cells[2].Style.BackColor = System.Drawing.Color.Red;

这似乎并没有改变任何可见的东西:

row.Cells[2].Style.ForeColor = System.Drawing.Color.Red;

如果无法更改背景,是否可以更改按钮上的字体?

使用.NET 2.0。

c# .net winforms .net-2.0
8个回答
33
投票

我错过了戴夫关于托马斯回答的注释,所以我只是发布了这个问题的简单解决方案。

将 Button 列的 FlatStyle 属性更新为 Popup,然后通过更新背景色和前景色,您可以更改按钮的外观。

DataGridViewButtonColumn c = (DataGridViewButtonColumn)myGrid.Columns["colFollowUp"];
c.FlatStyle = FlatStyle.Popup;
c.DefaultCellStyle.ForeColor = Color.Navy;
c.DefaultCellStyle.BackColor = Color.Yellow;

13
投票

根据 MSDN:

启用视觉样式后, 按钮列中的按钮已绘制 使用 ButtonRenderer 和单元格 通过属性指定的样式 比如DefaultCellStyle就没有 效果。

因此,您有两个选择之一。在您的 Program.cs 中,您可以删除此行:

Application.EnableVisualStyles();

这会让它起作用,但会让其他一切看起来都很糟糕。您的另一种选择(您不会喜欢这个)是继承 DataGridViewButtonCell 并重写 Paint() 方法。然后,您可以使用 ButtonRenderer 类上名为 DrawButton 的静态方法来自己绘制按钮。这意味着要弄清楚单元格当前处于哪种状态(单击、悬停等)并绘制角和边框等...您明白了,这是可行的,但非常痛苦。

如果您愿意,这里有一些示例代码可以帮助您入门:

 //Custom ButtonCell
 public class MyButtonCell : DataGridViewButtonCell
    {
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            ButtonRenderer.DrawButton(graphics, cellBounds, formattedValue.ToString(), new Font("Comic Sans MS", 9.0f, FontStyle.Bold), true, System.Windows.Forms.VisualStyles.PushButtonState.Default);
        }
    }

然后这是一个测试DataGridView:

DataGridViewButtonColumn c = new DataGridViewButtonColumn();
            c.CellTemplate = new MyButtonColumn();
            this.dataGridView1.Columns.Add(c);
            this.dataGridView1.Rows.Add("Click Me");

这个示例所做的就是绘制一个字体为“Comic Sans MS”的按钮。它不会考虑按钮的状态,正如您在运行应用程序时看到的那样。

祝你好运!


4
投票

DataGridView 中的默认按钮是使用 ButtonRenderer 绘制的,这使得覆盖它非常困难。如果我是你,我只需将按钮 FlatStyle 设置为“Popup”即可。

var buttonCell = (DataGridViewButtonCell)dataGridMappings.Rows[0].Cells[0];
buttonCell.FlatStyle = FlatStyle.Popup;
buttonCell.Style.BackColor = System.Drawing.Color.Red;

1
投票

如果这些单元格包含一个按钮,我确信您必须访问该按钮的属性 BackColor。 IE。获取单元格的值将其转换为按钮并设置其属性。


1
投票

我也遇到了同样的问题。

您所要做的就是

  1. 设置 DataGridView 的 ButtonColumn 的以下属性:

    FlatStyle = PopUp

  2. 设置单元格的颜色:

    row.Cells[2].Style.BackColor = System.Drawing.Color.Red;


0
投票

我认为您访问错误:

row.Cells[2].Style.BackColor = System.Drawing.Color.Red;

你说更新按钮的“轮廓”,但实际上是更新按钮后面的单元格。

这样的东西应该有效:

row.Cells[2].ButtonName.Style.BackColor = System.Drawing.Color.Red;

0
投票

使用自定义

DataGridViewButtonCell
方法从
Paint
派生自己的类绝对足够,无需删除
EnableVisualStyles()


0
投票

将单元格 FlatStyle 更改为以下内容:

var buttonCell = (DataGridViewButtonCell)dataGridMappings.Rows[0].Cells[0];
buttonCell.FlatStyle = FlatStyle.Flat;
buttonCell.Style.BackColor = System.Drawing.Color.Red;

它会起作用的。

© www.soinside.com 2019 - 2024. All rights reserved.