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

时间:2009-02-25 16:51:32

标签: c# .net winforms .net-2.0

我有一个大型DataGridView控件,它有几个单元格,其中大多数单元格都包含一个按钮。如何更改这些按钮的颜色?

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

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

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

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

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

使用.NET 2.0。

8 个答案:

答案 0 :(得分:26)

我错过了戴夫关于托马斯回答的说明,所以我只是简单地发布了这个问题。

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

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

答案 1 :(得分:13)

根据MSDN:

  

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

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

Application.EnableVisualStyles();

这将使它工作,但使其他一切看起来像垃圾。你的另一个选择,你不会喜欢这个,是继承 DataGridViewButtonCell 并覆盖Paint()方法。然后,您可以在名为 DrawButton ButtonRenderer 类上使用静态方法,自行绘制按钮。这意味着要弄清楚细胞当前处于哪种状态(点击,悬停等)并绘制角落和边界等等......你明白了,它是可行的,但却是巨大的痛苦。

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

 //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”的按钮。它不会考虑您在运行应用程序时看到的按钮状态。

好运!

答案 2 :(得分:2)

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

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

我认为您错误地访问了它:

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

你说更新按钮的“轮廓”,但它确实更新了按钮后面的单元格。

这样的事情应该有效:

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

答案 5 :(得分:0)

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

答案 6 :(得分:0)

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

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

它将起作用。

答案 7 :(得分:0)

我有同样的问题。

您要做的就是

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

    FlatStyle = PopUp

  2. 设置单元格的颜色:

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