选择行时如何在datagridview中选择特定单元格

选择行时如何在datagridview中选择特定单元格

本文介绍了选择行时如何在datagridview中选择特定单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这很简单,但我已经圈了。



当用户选择datagridview中的任何行时,我想将选择切换为第一列。 FullRowSelect或CellSelect的SelectionMode就可以了(我已经尝试了两种)。



我尝试过:



我已尝试触发各种各样的事件,不同类型的问题。



这不起作用,因为它太快更新了.CurrentCell(我认为):

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

...导致错误操作无效,因为它导致对SetCurrentCellAddressCore函数的可重入调用。



预先感谢任何指导。

...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.

推荐答案

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


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


这篇关于选择行时如何在datagridview中选择特定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:45