【问题标题】:How to disable (make read only) a cell in a DataGridView CheckBox column based on the value in other cells?如何根据其他单元格中的值禁用(只读)DataGridView CheckBox 列中的单元格?
【发布时间】:2013-09-04 14:34:35
【问题描述】:

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

请在下方找到我的DataGridView

如果 name 单元格在运行时为空,我要做的是禁用该复选框。

我尝试了很多方法,但在我检查后,单元格一直处于禁用状态(只读)。

我尝试过这样的事情:

private void sendDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (sendDGV.CurrentRow.Cells[1].Value != null)
    {
        sendDGV.CurrentRow.Cells[2].ReadOnly = false;
        sendDGV.Update();
    }
    else 
    {
        sendDGV.CurrentRow.Cells[2].ReadOnly = true;
        sendDGV.Update();
    }
}

【问题讨论】:

  • 你尝试了什么?使用 jquery 的最简单方法
  • @FlaviaObreja 看起来像一个表单应用程序,而不是一个 Web 应用程序。
  • 我的是一个表单应用程序

标签: c# winforms datagridview


【解决方案1】:

要处理列name 中的更改,您可以使用DataGridView.CellValueChanged 事件。 e 参数使您可以访问:

  • columnIndex 属性,因此您可以测试是否对 name 列(索引 1)进行了更改。
  • rowIndexproperty,因此您可以检索相关行并更改所需的值。

private void DataGridView1_CellValueChanged(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
    //second column
    if (e.ColumnIndex == 1) {
        object value = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        if (value != null && value.ToString() != string.Empty) {
            DataGridView1.Rows[e.RowIndex].Cells[2].ReadOnly = false;
        } else {
            DataGridView1.Rows[e.RowIndex].Cells[2].ReadOnly = true;
        }
    }
}

编辑

正如其他人所指出的,为了使 checkbox 禁用新添加的行(特别是如果 AllowUserToAddRow 属性设置为 true),您可以处理 RowAdded 事件:

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    DataGridView1.Rows[e.RowIndex].Cells[2].ReadOnly = true;
}

【讨论】:

    【解决方案2】:

    相当老的线程,但我认为您可以使用CellBeginEdit 事件并根据您的情况取消事件。它不是禁用列,而是取消编辑所需的列值。

    1) 订阅活动:

    this.dataGridView1.CellBeginEdit  += DataGridView1OnCellBeginEdit;
    

    2) 事件处理程序:

            private void DataGridView1OnCellBeginEdit(object sender, DataGridViewCellCancelEventArgs args)
        {
            var isTicked = this.dataGridView1.Rows[args.RowIndex].Cells[args.ColumnIndex].Value;
    
            args.Cancel = (isTicked is bool) && ((bool)isTicked);
        }
    

    我已使用该事件获得一个包含复选框。

    这意味着只有三列中的一列(“None”、“Read”、“Full”)可以为“true”

    【讨论】:

    • 这对我来说既优雅又新奇,喜欢它!
    【解决方案3】:

    您可以使用 DataGridView.CellValueChanged 事件:

     private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == 1 && dataGridView1[1, e.RowIndex].Value.ToString() != "")
                    dataGridView1[2, e.RowIndex].ReadOnly = false;
                else
                    dataGridView1[2, e.RowIndex].ReadOnly = true;
            }
        }
    

    但是为了首先禁用复选框,请确保使用设计器将列设置为只读,并且在 DataGridView.RowsAdded 事件中,为新创建的行设置复选框属性 ReadOnly = true:

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            dataGridView1[2, e.RowIndex].ReadOnly = true;
        }
    

    【讨论】:

    • 谢谢弗拉维亚。但是当我尝试这个例子时,我在第二个“if”条件下得到了空引用异常。所以我将其修改为类似于if (e.ColumnIndex == 1 && dataGridView1[1, e.RowIndex].Value != null) 的内容,现在效果很好。
    【解决方案4】:

    简单,Visual Studio中内置只读属性,标记为true

    【讨论】:

    • 这会更改整个列,而不是相关的特定行。
    猜你喜欢
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    相关资源
    最近更新 更多