我有一个带有 DataGridView 的程序,我的用户希望能够在其中拖放多个列。
拖放单个列很容易,这是内置行为。但是我找不到任何允许选择多个(相邻)列然后将它们拖到另一个列索引的设置。
有这个设置吗?如果没有,以前有没有人这样做过和/或如何做到?
(我已尝试通过 Google 对此进行广泛搜索,但找不到任何与我的问题特别匹配的内容)
从已经收到的回答和评论来看,事情似乎朝着错误的方向发展,所以我想澄清一下问题是什么:
拖放单个列不是问题,如上所述,我已经知道该怎么做
拖放一行也不是问题。虽然不同于拖放列,但我知道如何执行此操作。
拖放多个列(或行)是问题,...
具体,如何选择多个列,然后启动拖放
在您实际尝试这样做之前并不明显的是,为 DragDrop 选择单个列(或行)的默认 DataGridView UI 语义不能用于拖放多个列(或行)。 那 正是我正在寻找的如何做的可靠示例。我可能可以自己处理 Drop-completion 的事情。问题是多选和拖动启动。
最佳答案
应该是可以的。我已经完成了在网格之间拖放行的操作,因此应该应用相同的原则。这不是解决方案,但它应该是构建 block ,希望能让您开发一个解决方案。
首先,您需要处理 MouseMove 事件以查看是否发生拖动:
AddHandler dgv.MouseMove, AddressOf CheckForDragDrop
这是我的 CheckForDragDrop 代码。您需要检查是否选择了列以检测拖动是否开始。
Private Sub CheckForDragDrop(sender As Object, e As MouseEventArgs)
If e.Button = MouseButtons.Left Then
Dim ht As DataGridView.HitTestInfo = Me.HitTest(e.X, e.Y)
If (ht.Type = DataGridViewHitTestType.RowHeader OrElse ht.Type = DataGridViewHitTestType.Cell) AndAlso Me.Rows(ht.RowIndex).Selected Then
_ColumnDragInProgress = True
If Me.SelectedRows.Count > 1 Then
Me.DoDragDrop(Me.SelectedRows, DragDropEffects.All)
ElseIf Me.CurrentRow IsNot Nothing Then
Me.DoDragDrop(Me.CurrentRow, DragDropEffects.All)
End If
End If
End If
End Sub
您还需要处理 DragEnter 和 DragDrop 事件:
AddHandler dgv.DragEnter, AddressOf PerformDragEnter
AddHandler dgv.DragDrop, AddressOf PerformDragDrop
这是示例代码,但请记住我是在网格之间拖动行,而不是在网格内拖动列:
Private Sub PerformDragEnter(sender As Object, e As DragEventArgs)
If e.Data.GetDataPresent(GetType(DataGridViewRow)) OrElse e.Data.GetDataPresent(GetType(DataGridViewSelectedRowCollection)) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub PerformDragDrop(sender As Object, e As DragEventArgs)
Dim dscreen As Point = New Point(e.X, e.Y)
Dim dclient As Point = Me.PointToClient(dscreen)
Dim HitTest As HitTestInfo = Me.HitTest(dclient.X, dclient.Y)
'If dropped onto an Existing Row, use that Row as Sender - otherwise Sender=This DGV.
If HitTest.RowIndex >= 0 Then _Dropped_RowHit = Me.Rows(HitTest.RowIndex) Else _Dropped_RowHit = Nothing
Dim DroppedRows As New List(Of DataGridViewRow)
If e.Data.GetDataPresent(GetType(DataGridViewRow)) Then
DroppedRows.Add(e.Data.GetData(GetType(DataGridViewRow)))
ElseIf e.Data.GetDataPresent(GetType(DataGridViewSelectedRowCollection)) Then
Dim Rows As DataGridViewSelectedRowCollection = e.Data.GetData(GetType(DataGridViewSelectedRowCollection))
For Each rw As DataGridViewRow In Rows
DroppedRows.Add(rw)
Next
Else
DroppedRows = Nothing
Exit Sub
End If
If DroppedRows(0).DataGridView Is Me Then Exit Sub
e.Data.SetData(DroppedRows)
_DraggedFrom_Name = DroppedRows(0).DataGridView.Name
'Drop occurred, add your code to handle it here
End Sub
我知道这并不能准确回答您提出的问题,但希望它能让您朝着正确的方向开始。
* 更新 *
再想想这个,我相信这可以更简单。
首先,创建您自己的继承 DataGridView 的自定义 DataGridView,如下所示:
Public Class CustomDGV
Inherits DataGridView
将 DGV SelectionMode 设置为 DataGridViewSelectionMode.ColumnHeaderSelect 或您喜欢的任何允许列选择的模式
创建一些局部变量来跟踪拖动是否在进行中以及正在移动的内容,例如:
Dim ColDragInProgress as Boolean = False
Dim SelectedColumns as new List(Of Integer)
然后覆盖 MouseDown 和 MouseUp 事件以处理列拖动:
MouseDown 事件需要测试您是否单击了选定的列。如果是这样,您可能希望拖动标记并记录所有选定的列(以您喜欢的任何方式):
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
If e.Button = MouseButtons.Left Then
Dim ht As DataGridView.HitTestInfo
ht = Me.HitTest(e.X, e.Y)
If ht.ColumnIndex>=0 AndAlso Me.Columns(ht.ColumnIndex).Selected Then
ColDragInProgress = True
SelectedColumns = StoreAllSelectedCols()
Else
ColDragInProgress = False
MyBase.OnMouseDown(e)
End If
Else
MyBase.OnMouseDown(e) 'in all other cases call the base function
End If
End Sub
然后处理 MouseUp 事件以查看是否确实发生了拖动。如果您在未选择的列上使用 MouseUp,我假设它有。您当然可以只记录初始列并检查鼠标是否在不同的列上,即使已选中也是如此。
Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Right AndAlso ColDragInProgress Then
ColDragInProgress = False
Dim ht As DataGridView.HitTestInfo
ht = Me.HitTest(e.X, e.Y)
If ht.ColumnIndex>=0 AndAlso Not Me.Columns(ht.ColumnIndex).Selected Then 'On a Not selected Column so assume Drag Complete
Dim MoveToColumn As Integer = ht.ColumnIndex
PerformMovedColsToNewPosition(MoveToColumn, SelectedColumns) 'Your code to reorder cols as you want
Else
MyBase.OnMouseDown(e)
End If
Else
MyBase.OnMouseUp(e) 'now call the base Up-function
End If
End Sub
最后,您可以覆盖 OnMouseMove 以在像这样拖动时显示正确的鼠标指针:
Protected Overrides Sub OnMouseMove(e As System.Windows.Forms.MouseEventArgs)
If ColDragInProgress AndAlso e.Button = Windows.Forms.MouseButtons.Left Then
Dim dropEffect As DragDropEffects = Me.DoDragDrop(SelectedColumns, DragDropEffects.Copy)
Else
MyBase.OnMouseMove(e) 'let's do the base class the rest
End If
End Sub
关于c# - 在 DataGridView 中拖放多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58980639/