C# 更改 DataGridView 单元格中按钮的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/586829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Color of Button in DataGridView Cell
提问by Dave
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。
采纳答案by BFree
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 DataGridViewButtonCelland override the Paint() method. You can then use the static method on the ButtonRendererclass 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!!
祝你好运!!
回答by Tomas Pajonk
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。获取单元格的值将其转换为按钮并设置它的属性。
回答by Ryan Abbott
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;
回答by Mike Yasieniuk
I missed Dave's note on Tomas' answer so i am just posting the simple solution to this.
我错过了戴夫对托马斯回答的说明,所以我只是发布了一个简单的解决方案。
Update the FlatStyleproperty 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,然后通过更新背景色和前景色,您可以更改按钮的外观。
DataGridViewButtonColumn c = (DataGridViewButtonColumn)myGrid.Columns["colFollowUp"];
c.FlatStyle = FlatStyle.Popup;
c.DefaultCellStyle.ForeColor = Color.Navy;
c.DefaultCellStyle.BackColor = Color.Yellow;
回答by Hedgehog
It's absolutely enough to make your own class derived from DataGridViewButtonCell
with a custom Paint
method, without removing EnableVisualStyles()
.
DataGridViewButtonCell
使用自定义Paint
方法从其派生您自己的类绝对足够了,而无需删除EnableVisualStyles()
.
回答by NTDLS
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".
DataGridView 中的默认按钮是使用 ButtonRenderer 绘制的,这使得它很难被覆盖。如果我是你,我只需将按钮 FlatStyle 设置为“弹出”。
DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)dataGridMappings.Rows[0].Cells[0];
buttonCell.FlatStyle = FlatStyle.Popup;
buttonCell.Style.BackColor = System.Drawing.Color.Red;
回答by Developer .net
Change cell FlatStyle to the following:
将单元格 FlatStyle 更改为以下内容:
DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)dataGridMappings.Rows[0].Cells[0];
buttonCell.FlatStyle = FlatStyle.Flat;
buttonCell.Style.BackColor = System.Drawing.Color.Red;
It will work.
它会起作用。