DataGridViewTextBoxCell.ReadOnly = true,但仍然可以更改所选值

DataGridViewTextBoxCell.ReadOnly = true, but can still change the selected value

有同样的问题,但用户对我无法实现的答案感到满意。

我得到了一个包含许多列的 dataGridView,其中一个是 checkBoxColumn,它应该激活或停用带有文本的另一列:

现在,一旦加载了 dataGridView,我得到的代码就可以完美运行:我单击复选框,它们按预期运行。

问题是我希望 dataGridView 禁用在加载时未检查其对应的 Allergies 单元格的 AllergiesDescription 单元格。我编写了一个代码,该代码应该遍历行并在发现未经检查的 Allergies 单元格时执行禁用单元格代码。但是千方百计,它不起作用!它将所有需要的属性设置为单元格(如 readonly = true)但单元格仍然是可编辑的:

在下面分享我的代码:

    private void dataGridView_CellContentClick(object sender,
        DataGridViewCellEventArgs cellEvent)
    {
        if (cellEvent.ColumnIndex == AllergiesCheckBoxColumn.Index)
            toggleAllergiesDescriptionHabilitation(cellEvent.RowIndex);
    }

    private void toggleAllergiesDescriptionHabilitation(int rowIndex)
    {
        int columnIndex = AllergiesDescriptionTextBoxColumn.Index;
        DataGridViewRow row = dataGridView.Rows[rowIndex];
        var allergiesDescriptionCell = (DataGridViewTextBoxCell)row.Cells[columnIndex];
        var currentCell = (DataGridViewCheckBoxCell)dataGridView.CurrentCell;
        if ((bool)currentCell.EditedFormattedValue)
            enableCell(allergiesDescriptionCell);
        else
            disableCell(allergiesDescriptionCell);
    }

    private void enableCell(DataGridViewTextBoxCell cell)
    {
        cell.ReadOnly = false;
        cell.Style.BackColor = Color.White;
        cell.Style.ForeColor = Color.Black;
        cell.Style.SelectionBackColor = Color.Blue;
        cell.Style.SelectionForeColor = Color.White;
    }

    private void disableCell(DataGridViewTextBoxCell cell)
    {
        cell.ReadOnly = true;
        cell.Style.BackColor = Color.LightGray;
        cell.Style.ForeColor = Color.DarkGray;
        cell.Style.SelectionBackColor = Color.LightGray;
        cell.Style.SelectionForeColor = Color.DarkGray;
    }

正如我所说,该代码完美无缺,每当我选中一个 Allergies 复选框时,相应的 AllergiesDescription 单元格就会按预期启用或禁用。但是:

    public People(PersistenceManager persistenceManager)
    {
        InitializeComponent();
        //other stuff
        disableNotCheckedAllergiesDescription();
    }

    private void disableNotCheckedAllergiesDescription()
    {
        foreach (DataGridViewRow row in dataGridView.Rows)
        {
            Person person = (Person)row.DataBoundItem;
            if (person != null && !person.Allergies)
                disableNotCheckedAllergiesDescription(row);
        }
    }

    private void disableNotCheckedAllergiesDescription(DataGridViewRow row)
    {
        int columnIndex = AllergiesDescriptionTextBoxColumn.Index;
        var allergiesDescriptionCell = (DataGridViewTextBoxCell)row.Cells[columnIndex];
        disableCell(allergiesDescriptionCell);
    }

这不起作用,如您所见,它只对每个应该禁用的单元格执行disableCell代码,当我调试时,未检查的行正确进入方法disableCell,并且它们的只读值设置为正确,但它们仍然是可编辑的。

当您从构造函数内部调用它时,设置 DataGridViewCells 的 ReadOnly 属性 似乎来得太早了。尝试移动此电话:

disableNotCheckedAllergiesDescription();

改用 DataBindingComplete 事件。

private void dgv_DataBindingComplete(object sender, 
                                     DataGridViewBindingCompleteEventArgs e) {
  disableNotCheckedAllergiesDescription();
}