在 DataGridView 中拖放多列
Drag and Drop Multiple Columns in DataGridView
我有一个带有 DataGridView 的程序,我的用户希望能够在其中拖放多个列。
拖放单个列很容易,这是内置行为。但是我找不到任何允许 selecting 多个(相邻)列然后将它们拖到另一个列索引的设置。
有这个设置吗?如果没有,以前有没有人这样做过and/or怎么办?
(我已广泛尝试 Google,但找不到任何与我的问题特别匹配的内容)
从已经收到的回答和评论来看,事情似乎朝着错误的方向发展,所以我想澄清一下问题是什么:
拖放单个列不是问题,如上所述,我已经知道该怎么做
拖放一行也不是问题。虽然不同于拖放列,但我知道如何做到这一点。
拖放多个列(或行)是问题,...
具体,如何select多列然后启动DragDrop
在您实际尝试这样做之前并不明显的是 select 为 DragDrop 设置单个列(或行)的默认 DataGridView UI 语义无法用于拖放多个列(或行)。 那 就是我正在寻找如何做的可靠示例。我可能可以自己处理 Drop-completion 的事情。问题是多select离子和拖动启动。
应该是可以的。我已经完成了在网格之间拖放行的操作,因此应该应用相同的原则。这不是解决方案,但它是构建块,希望能让您开发一个。
首先,您需要处理 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
其他想法是使用 OnColumnDisplayIndexChanged 查找选定列之一何时移动,记住位置之前选定的列。并将它们放置在我们拖动的列的位置之后。对于这种情况,我们需要将选择模式从 FullRow/Cell 切换到列,但这仅适用于排序模式 DataGridViewColumnSortMode.Programmatic 也许可以帮助您 默认键是 "Alt" 以在此模式下拖放列
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
UI 为 DataGridView 编程的方式不支持直观的方式来执行此操作,因为如果您在选择内左键单击,它会更改选择。
最好的办法可能是检查 DataGridView.CellMouseDown
事件中的 e.Button
是否有右键或中键单击。然后可以访问 .SelectedCells
属性 来存储列选择,然后在 DataGridView.CellMouseUp
事件中执行 'drop' 操作。
我有一个带有 DataGridView 的程序,我的用户希望能够在其中拖放多个列。
拖放单个列很容易,这是内置行为。但是我找不到任何允许 selecting 多个(相邻)列然后将它们拖到另一个列索引的设置。
有这个设置吗?如果没有,以前有没有人这样做过and/or怎么办?
(我已广泛尝试 Google,但找不到任何与我的问题特别匹配的内容)
从已经收到的回答和评论来看,事情似乎朝着错误的方向发展,所以我想澄清一下问题是什么:
拖放单个列不是问题,如上所述,我已经知道该怎么做
拖放一行也不是问题。虽然不同于拖放列,但我知道如何做到这一点。
拖放多个列(或行)是问题,...
具体,如何select多列然后启动DragDrop
在您实际尝试这样做之前并不明显的是 select 为 DragDrop 设置单个列(或行)的默认 DataGridView UI 语义无法用于拖放多个列(或行)。 那 就是我正在寻找如何做的可靠示例。我可能可以自己处理 Drop-completion 的事情。问题是多select离子和拖动启动。
应该是可以的。我已经完成了在网格之间拖放行的操作,因此应该应用相同的原则。这不是解决方案,但它是构建块,希望能让您开发一个。
首先,您需要处理 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
其他想法是使用 OnColumnDisplayIndexChanged 查找选定列之一何时移动,记住位置之前选定的列。并将它们放置在我们拖动的列的位置之后。对于这种情况,我们需要将选择模式从 FullRow/Cell 切换到列,但这仅适用于排序模式 DataGridViewColumnSortMode.Programmatic 也许可以帮助您 默认键是 "Alt" 以在此模式下拖放列
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
UI 为 DataGridView 编程的方式不支持直观的方式来执行此操作,因为如果您在选择内左键单击,它会更改选择。
最好的办法可能是检查 DataGridView.CellMouseDown
事件中的 e.Button
是否有右键或中键单击。然后可以访问 .SelectedCells
属性 来存储列选择,然后在 DataGridView.CellMouseUp
事件中执行 'drop' 操作。