开发者

Force editing on DataGridView to one column

开发者 https://www.devze.com 2023-01-31 05:58 出处:网络
I want to only allow editing of one column in my DataGridView, but I want to allow the user to double click on any item in the row, and when the CellBeginEdit fires, force editing of my colum. I start

I want to only allow editing of one column in my DataGridView, but I want to allow the user to double click on any item in the row, and when the CellBeginEdit fires, force editing of my colum. I started by doing this:

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开发者_StackOverflow中文版
            dgvCaptions.ClearSelection()
            dgvCaptions.Rows(e.RowIndex).Cells(COL_CAPTION).Selected = True
            dgvCaptions.BeginEdit(False)
        End If
    End Sub

But this throws an error in the BeginEdit(False) line because 'Operation is not valid because it results in a reentrant call to the BeginEdit function.' which of course it will do, but that is what I want. Is there another way to do this?


instead of handling CellBeginEdit event, try by making other cells readonly and handling double click event. In double click handler set selected cell to your editable cell and then call BeginEdit


In the end I found this link to be useful and adapted it for my needs:

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

MSDN Forum Credit

0

精彩评论

暂无评论...
验证码 换一张
取 消