更改当前单元格,而不删除选择
Change Current Cell, Without Removing the Selection
在 datagridview 中选择多个单元格后,我希望当前单元格等于在 datagridview 中选择的第一个单元格。我遇到的问题是,在做出选择后(鼠标移开),我将当前单元格设置为第一个选定的单元格(me.datagridview.currentcell =),但这会删除 datagridview 中的所有其他选择.有谁知道在不删除 datagridview 选择的情况下更改当前单元格的方法。当前示例代码如下:
Private Sub DataGridView1_CellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp
a = 0
Do While a < Me.DataGridView1.RowCount
b = 0
Do While b < Me.DataGridView1.ColumnCount
If Me.DataGridView1.Rows(a).Cells(b).Selected = True Then
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(a).Cells(b)
GoTo skipstep
End If
b += 1
Loop
a += 1
Loop
skipstep:
End Sub
如果您查看 CurrentCell 属性 的源代码,您会发现它会在 之前调用 ClearSelection
SetCurrentCellAddressCore
。但是你不能调用 "SCCAC" 因为它被定义为 Protected
。所以我最好的建议是将 DGV 子类化并创建一个新的 public 方法。
Public Class UIDataGridView
Inherits DataGridView
Public Sub SetCurrentCell(cell As DataGridViewCell)
If (cell Is Nothing) Then
Throw New ArgumentNullException("cell")
'TODO: Add more validation:
'ElseIf (Not cell.DataGridView Is Me) Then
End If
Me.SetCurrentCellAddressCore(cell.ColumnIndex, cell.RowIndex, True, False, False)
End Sub
End Class
如果您不想子类化 DGV,那么反射是您唯一的选择。
Imports System.Reflection
Private Sub HandleCellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
Me.firstCell = If(((e.ColumnIndex > -1) AndAlso (e.RowIndex > -1)), Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex), Nothing)
End Sub
Private Sub HandleCellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp
If ((Not Me.firstCell Is Nothing) AndAlso (Me.firstCell.Selected AndAlso (Me.DataGridView1.SelectedCells.Count > 1))) Then
Dim type As Type = GetType(DataGridView)
Dim flags As BindingFlags = (BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public Or BindingFlags.NonPublic)
Dim method As MethodInfo = type.GetMethod("SetCurrentCellAddressCore", flags)
method.Invoke(Me.DataGridView1, {Me.firstCell.ColumnIndex, Me.firstCell.RowIndex, True, False, False})
Debug.WriteLine("First cell is current: {0}", {(Me.DataGridView1.CurrentCell Is Me.firstCell)})
End If
End Sub
Private firstCell As DataGridViewCell
PS:您是否忘记了用户可以使用键盘 select 单元格? ;)
在 datagridview 中选择多个单元格后,我希望当前单元格等于在 datagridview 中选择的第一个单元格。我遇到的问题是,在做出选择后(鼠标移开),我将当前单元格设置为第一个选定的单元格(me.datagridview.currentcell =),但这会删除 datagridview 中的所有其他选择.有谁知道在不删除 datagridview 选择的情况下更改当前单元格的方法。当前示例代码如下:
Private Sub DataGridView1_CellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp
a = 0
Do While a < Me.DataGridView1.RowCount
b = 0
Do While b < Me.DataGridView1.ColumnCount
If Me.DataGridView1.Rows(a).Cells(b).Selected = True Then
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(a).Cells(b)
GoTo skipstep
End If
b += 1
Loop
a += 1
Loop
skipstep:
End Sub
如果您查看 CurrentCell 属性 的源代码,您会发现它会在 之前调用 ClearSelection
SetCurrentCellAddressCore
。但是你不能调用 "SCCAC" 因为它被定义为 Protected
。所以我最好的建议是将 DGV 子类化并创建一个新的 public 方法。
Public Class UIDataGridView
Inherits DataGridView
Public Sub SetCurrentCell(cell As DataGridViewCell)
If (cell Is Nothing) Then
Throw New ArgumentNullException("cell")
'TODO: Add more validation:
'ElseIf (Not cell.DataGridView Is Me) Then
End If
Me.SetCurrentCellAddressCore(cell.ColumnIndex, cell.RowIndex, True, False, False)
End Sub
End Class
如果您不想子类化 DGV,那么反射是您唯一的选择。
Imports System.Reflection
Private Sub HandleCellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
Me.firstCell = If(((e.ColumnIndex > -1) AndAlso (e.RowIndex > -1)), Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex), Nothing)
End Sub
Private Sub HandleCellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp
If ((Not Me.firstCell Is Nothing) AndAlso (Me.firstCell.Selected AndAlso (Me.DataGridView1.SelectedCells.Count > 1))) Then
Dim type As Type = GetType(DataGridView)
Dim flags As BindingFlags = (BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public Or BindingFlags.NonPublic)
Dim method As MethodInfo = type.GetMethod("SetCurrentCellAddressCore", flags)
method.Invoke(Me.DataGridView1, {Me.firstCell.ColumnIndex, Me.firstCell.RowIndex, True, False, False})
Debug.WriteLine("First cell is current: {0}", {(Me.DataGridView1.CurrentCell Is Me.firstCell)})
End If
End Sub
Private firstCell As DataGridViewCell
PS:您是否忘记了用户可以使用键盘 select 单元格? ;)