Changing the button style of certain buttons in a DataGridView

715 Views Asked by At

I have a DataGridView with a Button column in it.

I want to be able to disable and enable the buttons in every row (and/or change the style of them), according to a cell value in the row.

So let's say we have the following code :

if(dataGridView1.Rows[1].Cells[1].Value.ToString()=="OK")
        {
            //button in the same row should be enabled or disabled
        }

Can this be done?

1

There are 1 best solutions below

0
On

You can inherit a DataGridViewDisableButtonCell class from DataGridViewButtonCell to use in your DGV, as explained on MSDN. Then you'll be able to enable/disable the button inside that cell with the following code:

DataGridViewDisableButtonCell buttonCell = (DataGridViewDisableButtonCell)dataGridView1.Rows[e.RowIndex].Cells["Buttons"];
buttonCell.Enabled = myEnableConditionMet ? true : false;