在DataGridView中拖放多列



我有一个带有DataGridView的程序,我的用户希望能够在其中拖放多个列。

拖放单个列很容易,这是内置行为。但我找不到任何允许选择多个(相邻(列,然后将它们拖动到另一个列索引的设置。

有这样的设置吗?如果没有,以前有人这样做过吗?


从已经收到的答案和评论来看,事情似乎朝着错误的方向发展,所以我想澄清问题是什么:

  • 拖放一列不是问题,如上所述,我已经知道如何进行

  • 拖放一行也不是问题。虽然与拖放列不同,但我知道如何做到这一点。

  • 拖放多个列(或行(是个问题。。。

  • 具体,如何选择多个列,然后启动DragDrop

在您真正尝试之前,不明显的是,为DragDrop选择单列(或行(的默认DataGridView UI语义无法用于拖放多列(或行将(这是我正在寻找的一个如何做的坚实的例子。我可能可以独自处理Drop完成的事情。这是多重选择和拖动启动的问题。

这应该是可能的。我这样做是为了在网格之间拖放行,所以同样的原则也应该适用。这不是一个解决方案,但它是一个构建块,希望它能让你开发一个。

首先,你需要处理鼠标移动事件,看看是否发生拖动:

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事件以查看是否真的发生了拖动。我假设如果你在一个未选中的列上鼠标悬停,它就会出现。当然,您可以只记录初始列,并检查鼠标是否在不同的列上,即使已选中。

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

其他想法是使用OnColumnDisplayIndexChanged来查找所选列中的一列何时移动,记住之前所选列的位置。然后将它们放置在我们拖动的列的位置。在这种情况下,我们需要将选择模式从FullRow/Cell切换到column,但这只适用于排序模式DataGridViewColumnSortMode。编程可能会帮助您。默认键是"Alt",用于在这种模式下拖放coumn

Dim newGrid As New MultiCollOrder
newGrid.Columns.Add("col_1", "col_1")
newGrid.Columns.Add("col_2", "col_2")
newGrid.Columns.Add("col_3", "col_3")
newGrid.Columns.Add("col_4", "col_4")
newGrid.Columns.Add("col_5", "col_5")
newGrid.Rows.Add({"1", "2", "3", "4", "5"})
newGrid.Rows.Add({"1a", "2a", "3a", "4a", "5a"})
newGrid.Rows.Add({"1b", "2b", "3b", "4b", "5b"})
newGrid.AllowUserToOrderColumns = True
Me.Controls.Add(newGrid)
newGrid.Dock = Windows.Forms.DockStyle.Fill
Public Class MultiCollOrder
Inherits System.Windows.Forms.DataGridView
Private _NewOrder As List(Of Integer) 'New Order
Private _orgSelectedOrder As New List(Of Integer) 'orginal order
Protected Overrides Sub OnCellClick(e As DataGridViewCellEventArgs)
If e.RowIndex >= 0 Then
'for rows
Me.SelectionMode = DataGridViewSelectionMode.FullRowSelect
For Each ecol As DataGridViewColumn In Me.Columns
ecol.SortMode = DataGridViewColumnSortMode.Automatic
Next
Else
'for column
_orgSelectedOrder.Clear() '
For Each ecol As DataGridViewColumn In (From esor As DataGridViewColumn In Me.SelectedColumns Order By esor.DisplayIndex Ascending)
_orgSelectedOrder.Add(ecol.Index)
Next
End If
MyBase.OnCellClick(e)
End Sub
Protected Overrides Sub OnColumnHeaderMouseClick(e As DataGridViewCellMouseEventArgs)
For Each ecol As DataGridViewColumn In Me.Columns
ecol.SortMode = DataGridViewColumnSortMode.Programmatic
Next
Me.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect
MyBase.OnColumnHeaderMouseClick(e)
End Sub
Protected Overrides Sub OnColumnDisplayIndexChanged(e As DataGridViewColumnEventArgs)
If Me.SelectedColumns.Count > 1 And e.Column.Selected And _NewOrder Is Nothing Then
_NewOrder = New List(Of Integer)
For Each ec As DataGridViewColumn In (From esor As DataGridViewColumn In Me.Columns Order By esor.DisplayIndex Ascending)
If ec.Index = e.Column.Index Then
For Each esc In _orgSelectedOrder
_NewOrder.Add(esc)
Next
Else
If ec.Selected = False Then _NewOrder.Add(ec.Index)
End If
Next
End If
MyBase.OnColumnDisplayIndexChanged(e)
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
If Not _NewOrder Is Nothing Then ' Apply New order
For x As Integer = 0 To _NewOrder.Count - 1
Me.Columns(_NewOrder(x)).Selected = False
Me.Columns(_NewOrder(x)).DisplayIndex = x
Next
_NewOrder = Nothing
End If
MyBase.OnPaint(e)
End Sub
End Class

为DataGridView编程UI的方式不支持直观的方法,因为如果在选择内部左键单击,它会更改选择。

您最好的选择可能是在DataGridView.CellMouseDown事件中选中e.Button以进行右键或中键单击。然后,您可以访问.SelectedCells属性来存储列选择,然后在DataGridView.CellMouseUp事件中执行"drop"操作。

最新更新