【问题标题】:Preventing Edits to specific rows in DataGridView防止编辑 DataGridView 中的特定行
【发布时间】:2009-02-11 21:54:32
【问题描述】:

我想阻止用户编辑或删除 datagridview 的前三行。

我该怎么做?

【问题讨论】:

    标签: c# .net-3.5 datagridview


    【解决方案1】:

    解决方案:

    private void dataGridView3_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) {
        if (e.RowIndex < 3) {
            e.Cancel = true;
        }
    }
    
    private void dataGridView3_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) {
        if (e.Row.Index < 3) {
            e.Cancel = true;
        }
    }
    

    【讨论】:

      【解决方案2】:

      一种方法是捕获 _CellBeginEdit 事件,检查是否允许对目标行进行任何编辑,如果不允许编辑则取消事件:

      private void dataGridViewIndexesSpecs_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) {

              if (e.RowIndex <= 3)
                  e.Cancel = true;
      
          }
      

      【讨论】:

      • 这会阻止他们也删除它吗?
      猜你喜欢
      • 2011-12-25
      • 1970-01-01
      • 2012-10-08
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      相关资源
      最近更新 更多