如何禁用 dataGridView 中不可点击的按钮(c# windows 应用程序)

How to Disable a button in dataGridView which is not clickable(c# windows application)

我发现了许多类似的问题和答案,但 none 帮助我解决了我的问题。

这是我遇到此问题的应用程序的屏幕截图 dataGridView with buttoncell

如果按钮文本显示 "ACCEPTED",我想禁用(或不可点击)按钮, 这是我所拥有的,但它不起作用

        private void Cellcontent()
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if ((row.Cells["Status"].Value.ToString()) == "ACCEPTED")
            {
                DataGridViewButtonCell cell = row.Cells["Status"] as DataGridViewButtonCell;
                cell.ReadOnly = true; //if 1 cell will be disabled (not clickable)

            }
        }
    }

我创建了一个小演示

https://github.com/manojsethi/DataGridViewDisableButton

从 MSDN 收集的信息

希望对您有所帮助

如果您不介意在值不是 ACCEPTED 时使用不同的单元格,则可以执行以下操作:

 foreach (DataGridViewRow item in dataGridView1.Rows.OfType<DataGridViewRow>().Where(c => c.Cells[buttonCol.Index].Value != null && c.Cells[buttonCol.Index].Value.ToString() == "ACCEPTED"))
 {
     //you can replace the button with a textbox cell that contains the rejected value
     item.Cells[buttonCol.Index] = new DataGridViewTextBoxCell { Value = "ACCEPTED" }; 
     item.Cells[buttonCol.Index].ReadOnly = true;
     //note that you have to make a new button cell and replace the rejected ones if the status is updated later on to be able to use the button.
 }

希望这对您有所帮助。