选择行时如何在datagridview中选择特定单元格 [英] How to select a specific cell in datagridview when row is selected
问题描述
我认为这很简单,但我已经圈了。
当用户选择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.
推荐答案
使用RowHeaderMouseClick
活动,
问题m是当您使用行标题选择整行时,整个行(所有单元格)被选中,CurrentCell
属性未按预期工作以选择特定单元格选择行,
i通过临时选择下一行或上一行的单元格然后切换回当前行来做一个解决方法,通过这样做,它运行良好。我并不是说这是唯一的解决方案,可能还有其他解决方案。
试试这个,
useRowHeaderMouseClick
Event,
The problem is when you select the entire row using row header, the entire row (all cells) gets selected and theCurrentCell
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,谢谢。我认为你对CurrentCell属性没有按预期工作是正确的。我终于找到了一种方法来完成这项工作,并设法避免选择不同的行然后返回:
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
这篇关于选择行时如何在datagridview中选择特定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!