在DataGridView的小区变更按钮的颜色 [英] Change Color of Button in DataGridView Cell

查看:586
本文介绍了在DataGridView的小区变更按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这改变了按钮的纲要而不是按钮本身。

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?

使用.NET 2.0。

Using .NET 2.0.

推荐答案

按<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewbuttoncell%28VS.80%29.aspx">MSDN:

当启用视觉样式时,   在按钮栏按钮都被漆成   使用ButtonRenderer,和细胞   通过属性指定的样式   如DefaultCellStyle没有   的效果。

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.

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

Therefore, you have one of two choices. In your Program.cs you can remove this line:

Application.EnableVisualStyles();

这将使它的工作,但让一切看起来像废话。您的其他选项,你不会喜欢这一个,是从 DataGridViewButtonCell 继承并覆盖paint()方法。然后,您可以使用静态方法的 ButtonRenderer 类叫做 DrawButton ,给自己画的按钮。这意味着,搞清楚该状态下,电池目前处于(点击,悬停等)和绘画的弯道和边界等..你的想法,这是可行的,但一个巨大的痛苦。

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.

如果你想,虽然,这里只是一些示例code,让你开始:

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);
        }
    }

然后这里有一个测试DataGridView的:

Then here's a test DataGridView:

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

所有这些样本中,是画一个按钮,字体为Comic Sans字体MS。它没有考虑到按钮的状态,当您运行应用程序,你会看到的。

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.

GOOD LUCK !!

GOOD LUCK!!

这篇关于在DataGridView的小区变更按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆