Click here to Skip to main content
14,695,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I thought this would be simple, but I've gone in circles.

When the user selects any row in a datagridview I want to switch the selection to the first column. A SelectionMode of either FullRowSelect or CellSelect would be fine (I've tried both).

What I have tried:

I've tried triggering on various events, with different sorts of issues.

This doesn't work because it updates the .CurrentCell too soon (I think):
Private Sub dgvDemo_SelectionChanged(sender As Object, e As EventArgs) Handles dgvDemo.SelectionChanged
        Dim dgv As DataGridView = sender
        dgv.CurrentCell = dgv(dgv.CurrentRow.Index, 0)
    End Sub


And this...
Private Sub dgvDemo_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDemo.RowEnter
        Dim dgv As DataGridView = sender
        If (Not dgv.CurrentRow Is Nothing) Then
            dgv.CurrentCell = dgv(dgv.CurrentRow.Index, 0)
        End If
...leads to the error "Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function."

Thanks in advance for any guidance.
Posted
Updated 14-Jun-16 11:02am

use RowHeaderMouseClick Event,
The problem is when you select the entire row using row header, the entire row (all cells) gets selected and the CurrentCell property is not working as expected to select a particular cell in the selected row,
i did a workaround by selecting the next or prev row's cell temporarily and then switching back to the current Row, by doing so, it works good. I am not saying this is the only solution, there might be some other, explore it.

try this,

Private Sub dataGridView1_RowHeaderMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs)
    Dim currentRow As Integer = e.RowIndex
    Dim rowsCount As Integer = dataGridView1.Rows.Count
    Dim tempSelectRowIndex As Integer = 0
    If ((currentRow + 1)  _
                = rowsCount) Then
        tempSelectRowIndex = (currentRow - 1)
    ElseIf (currentRow < rowsCount) Then
        tempSelectRowIndex = (currentRow + 1)
    End If

    dataGridView1.CurrentCell = dataGridView1.Rows(tempSelectRowIndex).Cells(0)
    dataGridView1.CurrentCell = dataGridView1.Rows(currentRow).Cells(0)
End Sub
   
Karthik, thank you. I think you're right about the CurrentCell property not working as expected. I finally found a way to make this work, and managed to avoid selecting a different row and then back:

Private Sub dgvDemo_SelectionChanged(sender As Object, e As EventArgs) Handles dgvDemo.SelectionChanged
        Dim dgv As DataGridView = sender
        If (Not dgv.CurrentCell Is Nothing) Then
            If (dgv.CurrentCell.ColumnIndex > 0) Then
                dgv.CurrentCell = dgv.Rows(dgv.CurrentRow.Index).Cells(0)
            End If
        End If
    End Sub
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900