Munashe Munashe - 4 months ago 85
C# Question

Change color of button in DataGridViewButtonColumn

I have searched high and low for an answer to this question.The answer on this post: Change Color of Button in DataGridView Cell does not answer my question as it regards font.

I have tried the following:

DataGridViewRow r = dataGridView.Rows[0];
r.Cells[1].Style.BackColor = Color.Red;

I have also tried:

DataGridViewButtonColumn btnCOl = new DataGridViewButtonColumn();
btnCOl.FlatStyle = FlatStyle.Popup;
DataGridViewRow r = dataGridView.Rows[0];
r.Cells[1].Style = new DataGridViewCellStyle { BackColor = Color.LightBlue };

Still to no avail.

I also commented out this line:

// Application.EnableVisualStyles();

If there is anyone who knows how to change the background color of a single button in a DataGridViewButtonColumn, please help.

I want to set different colors for the cells in the column e.g. some will be red while others will be green. I don't want to set the color for the whole column.


Try This

DataGridViewButtonCell bc = new DataGridViewButtonCell();
bc.FlatStyle = FlatStyle.Flat;
bc.Style.BackColor = Color.AliceBlue;

You can than assign this cell to the row you need

Here is a small example with a DataGridView dgvSample Already inserted in form

for (int i = 0; i <= 10; i++)
    DataGridViewRow fr = new DataGridViewRow();

    DataGridViewButtonCell bc = new DataGridViewButtonCell();
    bc.FlatStyle = FlatStyle.Flat;

    if (i % 2 == 0)
        bc.Style.BackColor = Color.Red;
        bc.Style.BackColor = Color.Green;

    fr.Cells[0] = bc;