更改DataGridView中某些按钮的按钮样式

本文关键字:按钮 样式 DataGridView 更改 | 更新日期: 2023-09-27 18:21:08

我有一个DataGridView,里面有一个Button列。

我希望能够根据行中的单元格值禁用和启用每行中的按钮(和/或更改它们的样式)。

假设我们有以下代码:

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

这能做到吗?

更改DataGridView中某些按钮的按钮样式

您可以从DataGridViewButtonCell继承一个DataGridViewDisableButtonCell类,以便在DGV中使用,如MSDN上所述。然后,您可以使用以下代码启用/禁用该单元格内的按钮:

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