Change Color of Button in DataGridView Cell

2020-02-09 03:56发布

问题:

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?

Using .NET 2.0.

回答1:

As per 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.

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

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.

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:

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.

GOOD LUCK!!



回答2:

I missed Dave's note on Tomas' answer so i am just posting the simple solution to this.

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.

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


回答3:

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".

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


回答4:

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.



回答5:

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:

It's absolutely enough to make your own class derived from DataGridViewButtonCell with a custom Paint method, without removing EnableVisualStyles().



回答7:

Change cell FlatStyle to the following:

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

It will work.