【问题标题】:Read Only cell in DataGridView in vb.netvb.net中DataGridView中的只读单元格
【发布时间】:2016-05-20 16:49:27
【问题描述】:

我只想将 datagridview 中的一个单元格设为只读,我尝试了以下方法,但都没有成功。

dgv_parametersetting(2, 0).ReadOnly = True
dgv_parametersetting.Rows(0).Cells(2).ReadOnly = True

For Each r As DataGridViewRow In dgv_parametersetting.Rows
      If r.Cells(2).Value = 0 Then
           r.Cells(2).ReadOnly = True
      End If
Next

谁能给我提供使datagridview视图中的特定单元格只读的语法。

【问题讨论】:

    标签: vb.net datagridview


    【解决方案1】:

    您可能想试试这个 - 代码项目中的代码 - :

       Private Sub dgv_CellBeginEdit(sender As Object, e As  DataGridViewCellCancelEventArgs) Handles dgv.CellBeginEdit
    
     If e.RowIndex = 0 And e.ColumnIndex = 1 Then
        e.Cancel = True
     End If
    
    End Sub
    

    【讨论】:

    • 这通过阻止用户进入单元格而作为例外工作
    【解决方案2】:

    从未尝试过 ReadOnly 属性,但我想您可以通过 CellValidating-event 获得相同的结果。使用上面的代码 + e.Cancel

    【讨论】:

      【解决方案3】:

      您可以处理RowsAdded 之类的事件:

      Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs)
      Handles DataGridView1.RowsAdded
       If DataGridView1.Rows.Count > 0 AndAlso DataGridView1.ColumnCount >= 2 Then
          DataGridView1.Rows(e.RowIndex).Cells(2).ReadOnly = True
       End If
       End Sub  
      

      【讨论】:

        猜你喜欢
        • 2010-10-30
        • 1970-01-01
        • 2012-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多