【问题标题】:Setting cell.ReadOnly has no effect设置 cell.ReadOnly 无效
【发布时间】:2011-03-15 14:45:22
【问题描述】:

我有一个DataGridView,我想通过设置ReadOnly = true 禁用一些单元格/行。

有时这不起作用并且单元格/行仍然可编辑的原因可能是什么?

还有其他方法可以防止编辑特定的行或单元格吗?是否可以防止点击或输入单元格?

【问题讨论】:

    标签: c# .net winforms datagridview


    【解决方案1】:

    您可以使用CellBeginEdit 事件阻止编辑。如果不想编辑单元格,可以取消编辑。例如,如果您只希望第一列可编辑,您可以这样做:

    private void dataGridView1_CellBeginEdit(object sender, 
       DataGridViewCellCancelEventArgs e)
    {
       if (e.ColumnIndex != 0) 
       { 
          e.Cancel = true;
       }
    }
    

    【讨论】:

      【解决方案2】:

      在将只读值设置为 true 后尝试运行 datagridview.Refresh()。

      【讨论】:

        【解决方案3】:

        一种可能性是,如果您设置 DataGridView 的 ReadOnly 属性(在代码中),则需要重置列的 ReadOnly:

        this.dgv.Columns[0].ReadOnly = true; 
        this.dgv.ReadOnly = false; 
        // Need to reset the column's ReadOnly state:
        this.dgv.Columns[0].ReadOnly = true; 
        

        你也可以set whole rows为只读

        【讨论】:

          猜你喜欢
          • 2011-10-17
          • 2015-12-31
          • 2021-02-27
          • 2019-10-27
          • 2013-01-04
          • 2017-01-06
          • 1970-01-01
          • 2016-07-10
          • 1970-01-01
          相关资源
          最近更新 更多