I have a large DataGridView control that has several cells most of which contain a button. How can I change the color of those buttons?
我有一个大的DataGridView控件,它有几个单元格,其中大部分都包含一个按钮。如何更改这些按钮的颜色?
This changes the "outline" of the button but not the button itself.
这会更改按钮的“轮廓”,但不会更改按钮本身。
row.Cells[2].Style.BackColor = System.Drawing.Color.Red;
This doesn't seem to change anything that's visible:
这似乎没有改变任何可见的东西:
row.Cells[2].Style.ForeColor = System.Drawing.Color.Red;
If it's not possible to change the background, is it possible to change the font on the button?
如果无法更改背景,是否可以更改按钮上的字体?
Using .NET 2.0.
使用.NET 2.0。
6 个解决方案
#1
13
As per MSDN:
根据MSDN:
When visual styles are enabled, the buttons in a button column are painted using a ButtonRenderer, and cell styles specified through properties such as DefaultCellStyle have no effect.
启用视觉样式时,按钮列中的按钮使用ButtonRenderer绘制,而通过DefaultCellStyle等属性指定的单元格样式不起作用。
Therefore, you have one of two choices. In your Program.cs you can remove this line:
因此,您有两种选择之一。在Program.cs中,您可以删除此行:
Application.EnableVisualStyles();
which will make it work, but make everything else look like crap. Your other option, and you're not going to like this one, is to inherit from DataGridViewButtonCell and override the Paint() method. You can then use the static method on the ButtonRenderer class called DrawButton, to paint the button yourself. That means figuring out which state the cell currently is in (clicked, hover etc.) and painting the corners and borders etc... You get the idea, it's doable, but a HUGE pain.
这将使它工作,但让其他一切看起来像垃圾。你的另一个选择,你不会喜欢这个,是从DataGridViewButtonCell继承并覆盖Paint()方法。然后,您可以在名为DrawButton的ButtonRenderer类上使用静态方法,自己绘制按钮。这意味着要弄清楚细胞当前处于哪种状态(点击,悬停等)并绘制角落和边界等等......你明白了,这是可行的,但却是巨大的痛苦。
If you want to though, here's just some sample code to get you started:
如果您愿意,这里只是一些示例代码来帮助您入门:
//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);
}
}
Then here's a test DataGridView:
然后这是一个测试DataGridView:
DataGridViewButtonColumn c = new DataGridViewButtonColumn();
c.CellTemplate = new MyButtonColumn();
this.dataGridView1.Columns.Add(c);
this.dataGridView1.Rows.Add("Click Me");
All this sample does, is paint a button with the font being "Comic Sans MS". It doesn't take into account the state of the button as you'll see when you run the app.
所有这些示例都是,绘制一个字体为“Comic Sans MS”的按钮。它不会考虑您在运行应用程序时看到的按钮状态。
GOOD LUCK!!
#2
26
I missed Dave's note on Tomas' answer so i am just posting the simple solution to this.
我错过了Dave关于Tomas回答的说明,所以我只是发布了简单的解决方案。
Update the FlatStyle property of the Button column to Popup and then by updating the backcolor and forecolor you can change the appearance of the button.
将Button列的FlatStyle属性更新为Popup,然后通过更新背景颜色和forecolor,您可以更改按钮的外观。
DataGridViewButtonColumn c = (DataGridViewButtonColumn)myGrid.Columns["colFollowUp"];
c.FlatStyle = FlatStyle.Popup;
c.DefaultCellStyle.ForeColor = Color.Navy;
c.DefaultCellStyle.BackColor = Color.Yellow;
#3
1
If these cells contain a button I am quite sure you have to access that button's property BackColor. Ie. get the value of the cell convert it to a button and set it's property.
如果这些单元格包含一个按钮,我很确定你必须访问该按钮的属性BackColor。 IE浏览器。获取单元格的值将其转换为按钮并设置其属性。
#4
1
The default button in a DataGridView is drawn using the ButtonRenderer which makes it quite difficult to override. if I were you, I'd just set the button FlatStyle to "Popup".
使用ButtonRenderer绘制DataGridView中的默认按钮,这使得覆盖非常困难。如果我是你,我只需将FlatStyle按钮设置为“Popup”。
DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)dataGridMappings.Rows[0].Cells[0];
buttonCell.FlatStyle = FlatStyle.Popup;
buttonCell.Style.BackColor = System.Drawing.Color.Red;
#5
0
I think you're accessing it wrong:
我认为你错了:
row.Cells[2].Style.BackColor = System.Drawing.Color.Red;
you say updates the "outline" of the button, but it's really updating the cell behind the button.
你说更新按钮的“轮廓”,但它确实更新了按钮后面的单元格。
something like this should work:
像这样的东西应该工作:
row.Cells[2].ButtonName.Style.BackColor = System.Drawing.Color.Red;
#6
0
It's absolutely enough to make your own class derived from DataGridViewButtonCell
with a custom Paint
method, without removing EnableVisualStyles()
.
使用自定义Paint方法从DataGridViewButtonCell派生自己的类是绝对足够的,而无需删除EnableVisualStyles()。
#1
13
As per MSDN:
根据MSDN:
When visual styles are enabled, the buttons in a button column are painted using a ButtonRenderer, and cell styles specified through properties such as DefaultCellStyle have no effect.
启用视觉样式时,按钮列中的按钮使用ButtonRenderer绘制,而通过DefaultCellStyle等属性指定的单元格样式不起作用。
Therefore, you have one of two choices. In your Program.cs you can remove this line:
因此,您有两种选择之一。在Program.cs中,您可以删除此行:
Application.EnableVisualStyles();
which will make it work, but make everything else look like crap. Your other option, and you're not going to like this one, is to inherit from DataGridViewButtonCell and override the Paint() method. You can then use the static method on the ButtonRenderer class called DrawButton, to paint the button yourself. That means figuring out which state the cell currently is in (clicked, hover etc.) and painting the corners and borders etc... You get the idea, it's doable, but a HUGE pain.
这将使它工作,但让其他一切看起来像垃圾。你的另一个选择,你不会喜欢这个,是从DataGridViewButtonCell继承并覆盖Paint()方法。然后,您可以在名为DrawButton的ButtonRenderer类上使用静态方法,自己绘制按钮。这意味着要弄清楚细胞当前处于哪种状态(点击,悬停等)并绘制角落和边界等等......你明白了,这是可行的,但却是巨大的痛苦。
If you want to though, here's just some sample code to get you started:
如果您愿意,这里只是一些示例代码来帮助您入门:
//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);
}
}
Then here's a test DataGridView:
然后这是一个测试DataGridView:
DataGridViewButtonColumn c = new DataGridViewButtonColumn();
c.CellTemplate = new MyButtonColumn();
this.dataGridView1.Columns.Add(c);
this.dataGridView1.Rows.Add("Click Me");
All this sample does, is paint a button with the font being "Comic Sans MS". It doesn't take into account the state of the button as you'll see when you run the app.
所有这些示例都是,绘制一个字体为“Comic Sans MS”的按钮。它不会考虑您在运行应用程序时看到的按钮状态。
GOOD LUCK!!
#2
26
I missed Dave's note on Tomas' answer so i am just posting the simple solution to this.
我错过了Dave关于Tomas回答的说明,所以我只是发布了简单的解决方案。
Update the FlatStyle property of the Button column to Popup and then by updating the backcolor and forecolor you can change the appearance of the button.
将Button列的FlatStyle属性更新为Popup,然后通过更新背景颜色和forecolor,您可以更改按钮的外观。
DataGridViewButtonColumn c = (DataGridViewButtonColumn)myGrid.Columns["colFollowUp"];
c.FlatStyle = FlatStyle.Popup;
c.DefaultCellStyle.ForeColor = Color.Navy;
c.DefaultCellStyle.BackColor = Color.Yellow;
#3
1
If these cells contain a button I am quite sure you have to access that button's property BackColor. Ie. get the value of the cell convert it to a button and set it's property.
如果这些单元格包含一个按钮,我很确定你必须访问该按钮的属性BackColor。 IE浏览器。获取单元格的值将其转换为按钮并设置其属性。
#4
1
The default button in a DataGridView is drawn using the ButtonRenderer which makes it quite difficult to override. if I were you, I'd just set the button FlatStyle to "Popup".
使用ButtonRenderer绘制DataGridView中的默认按钮,这使得覆盖非常困难。如果我是你,我只需将FlatStyle按钮设置为“Popup”。
DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)dataGridMappings.Rows[0].Cells[0];
buttonCell.FlatStyle = FlatStyle.Popup;
buttonCell.Style.BackColor = System.Drawing.Color.Red;
#5
0
I think you're accessing it wrong:
我认为你错了:
row.Cells[2].Style.BackColor = System.Drawing.Color.Red;
you say updates the "outline" of the button, but it's really updating the cell behind the button.
你说更新按钮的“轮廓”,但它确实更新了按钮后面的单元格。
something like this should work:
像这样的东西应该工作:
row.Cells[2].ButtonName.Style.BackColor = System.Drawing.Color.Red;
#6
0
It's absolutely enough to make your own class derived from DataGridViewButtonCell
with a custom Paint
method, without removing EnableVisualStyles()
.
使用自定义Paint方法从DataGridViewButtonCell派生自己的类是绝对足够的,而无需删除EnableVisualStyles()。