gpt4 book ai didi

vb.net - 更改当前单元格,而不删除选择

转载 作者:行者123 更新时间:2023-12-05 02:22:25 26 4
gpt4 key购买 nike

在 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:你忘了用户可以使用键盘选择单元格吗? ;)

关于vb.net - 更改当前单元格,而不删除选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29467085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com