我想只允许编辑DataGridView中的一列,但我希望允许用户双击行中的任何项目,当CellBeginEdit触发时,强制编辑我的列。我开始这样做:
Private Sub dgvCaptions_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgvCaptions.CellBeginEdit
If e.ColumnIndex <> COL_CAPTION Then
e.Cancel = True
dgvCaptions.ClearSelection()
dgvCaptions.Rows(e.RowIndex).Cells(COL_CAPTION).Selected = True
dgvCaptions.BeginEdit(False)
End If
End Sub
但是这会在BeginEdit(False)行中抛出一个错误,因为'Operation无效,因为它导致对BeginEdit函数的重入调用。这当然会做,但这就是我想要的。还有另一种方法吗?
答案 0 :(得分:1)
而不是处理CellBeginEdit
事件,请尝试将其他单元格设为只读并处理double click
事件。在double click
处理程序中,将所选单元格设置为可编辑单元格,然后调用BeginEdit
答案 1 :(得分:0)
最后,我发现此链接很有用,并根据我的需要进行了调整:
Delegate Sub SetColumnIndex(ByVal i As Integer)
Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
If Me.dataGridView1.CurrentCell.ColumnIndex <> Me.dataGridView1.Columns.Count - 1 Then
Dim nextindex As Integer = Math.Min(Me.dataGridView1.Columns.Count - 1, Me.dataGridView1.CurrentCell.ColumnIndex + 1)
Dim method As New SetColumnIndex(AddressOf Mymethod)
Me.dataGridView1.BeginInvoke(method, nextindex)
End If
End Sub
Private Sub Mymethod(ByVal columnIndex As Integer)
Me.dataGridView1.CurrentCell = Me.dataGridView1.CurrentRow.Cells(columnIndex)
Me.dataGridView1.BeginEdit(True)
End Sub