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

如何禁用dataGridView中不可单击的按钮(c#windows应用程序),c#,button,windows-applications,C#,Button,Windows Applications,我发现了许多类似的问题和答案,但没有一个能帮助我解决问题 这是我面临这个问题的应用程序的屏幕截图 如果按钮文本显示“已接受”,我想禁用(或不可单击)按钮, 这是我所拥有的,但这不起作用 private void Cellcontent() { foreach (DataGridViewRow row in dataGridView1.Rows) { if ((row.Cells["Status"].Value.To

我发现了许多类似的问题和答案,但没有一个能帮助我解决问题

这是我面临这个问题的应用程序的屏幕截图

如果按钮文本显示“已接受”,我想禁用(或不可单击)按钮, 这是我所拥有的,但这不起作用

        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)

            }
        }
    }

我已经创建了一个小演示

从MSDN收集的信息


希望它有帮助

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

foreach(dataGridView1.Rows.OfType()中的DataGridViewRow项,其中(c=>c.Cells[buttonCol.Index].Value!=null&&c.Cells[buttonCol.Index].Value.ToString()
{
//可以使用包含拒绝值的文本框单元格替换按钮
item.Cells[buttonCol.Index]=新的DataGridViewTextBoxCell{Value=“ACCEPTED”};
item.Cells[buttonCol.Index].ReadOnly=true;
//请注意,如果稍后更新状态以使用按钮,则必须创建一个新的按钮单元并替换被拒绝的按钮单元。
}

希望这有帮助。

好主意。工作完美。谢谢兄弟:)
 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.
 }