DataGridViewTextBoxCell.ReadOnly = true,但仍可以更改所选值 [英] DataGridViewTextBoxCell.ReadOnly = true, but can still change the selected value

查看:185
本文介绍了DataGridViewTextBoxCell.ReadOnly = true,但仍可以更改所选值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataGridViewComboBoxCell.ReadOnly = true,但仍然可以更改所选值有相同的问题,但用户对我无法实现的答案感到满意.

DataGridViewComboBoxCell.ReadOnly = true, but can still change the selected value has the same question but user was satisfied with an answer I can't implement.

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

I got a dataGridView with many columns, one of which is a checkBoxColumn, which should activate or deactivate another column with text:

现在,一旦加载dataGridView,我得到的代码就可以完美工作:我单击复选框,它们将按预期工作.

Now, the code I got works perfect once the dataGridView is loaded: I click on the checkboxes and they work as expected.

问题是我希望dataGridView禁用在加载时间中未检查其对应过敏单元的AllergiesDescription单元.我编写了一个代码,该代码应遍历各行,并在找到未经检查的过敏"单元格时执行禁用单元格代码.但无论如何,它是行不通的!它将所有所需的属性设置为该单元格(例如readonly = true),但该单元格仍可

The problem is that I want the dataGridView to disable AllergiesDescription cells that don't have their corresponding Allergies cell checked in load time. I wrote a code that should iterate over the rows and execute the disable cell code whenever an unchecked Allergies cell is found. But against all odds, it doesn't work! It sets all wanted properties to the cell (like readonly = true) but still the cell is editable:

在下面共享我的代码:

    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;
    }

正如我所说,该代码非常完美,每当我选中过敏"复选框时,相应的AllergiesDescription单元就会按预期启用或禁用.但是:

That code works perfect as I said, whenever I check an Allergies checkBox the corresponding AllergiesDescription cell enables or disables as expected. But:

    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,并且其readonly值正确设置为true ,但它们仍可编辑.

This doesn't work, as you can see, it only executes the disableCell code for each cell that should be disabled, when I debug, the unchecked rows enter the method disableCell correctly, and their readonly value is set to true correctly, but they are still editable.

推荐答案

从构造函数内部调用DataGridViewCells的ReadOnly属性似乎还为时过早.尝试移动此呼叫:

Setting the ReadOnly property of the DataGridViewCells seems to come too early when you call it from inside the constructor. Try moving this call:

disableNotCheckedAllergiesDescription();

改为DataBindingComplete事件.

to the DataBindingComplete event instead.

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

这篇关于DataGridViewTextBoxCell.ReadOnly = true,但仍可以更改所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆