[英]How to drag and drop row within the same datagridview
In a Windows App (Visual Studio)(VB) how do you drag and drop a single row to another postition to allow for the user to re-order the row? 在Windows应用程序(Visual Studio)(VB)中,如何将单个行拖放到另一个帖子以允许用户重新排序该行? I haven't found any worthy examples for this yet. 我还没有找到任何有价值的例子。
Here is a vb version from this C# answer: How could I Drag and Drop DataGridView Rows under each other? 这是来自这个C#答案的vb版本: 我怎样才能将DataGridView行拖放到彼此之下?
The form class variables: 表单类变量:
Private fromIndex As Integer
Private dragIndex As Integer
Private dragRect As Rectangle
The drag events: 拖动事件:
Private Sub DataGridView1_DragDrop(ByVal sender As Object, _
ByVal e As DragEventArgs) _
Handles DataGridView1.DragDrop
Dim p As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
dragIndex = DataGridView1.HitTest(p.X, p.Y).RowIndex
If (e.Effect = DragDropEffects.Move) Then
Dim dragRow As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
DataGridView1.Rows.RemoveAt(fromIndex)
DataGridView1.Rows.Insert(dragIndex, dragRow)
End If
End Sub
Private Sub DataGridView1_DragOver(ByVal sender As Object, _
ByVal e As DragEventArgs) _
Handles DataGridView1.DragOver
e.Effect = DragDropEffects.Move
End Sub
The mouse events: 鼠标事件:
Private Sub DataGridView1_MouseDown(ByVal sender As Object, _
ByVal e As MouseEventArgs) _
Handles DataGridView1.MouseDown
fromIndex = DataGridView1.HitTest(e.X, e.Y).RowIndex
If fromIndex > -1 Then
Dim dragSize As Size = SystemInformation.DragSize
dragRect = New Rectangle(New Point(e.X - (dragSize.Width / 2), _
e.Y - (dragSize.Height / 2)), _
dragSize)
Else
dragRect = Rectangle.Empty
End If
End Sub
Private Sub DataGridView1_MouseMove(ByVal sender As Object, _
ByVal e As MouseEventArgs) _
Handles DataGridView1.MouseMove
If (e.Button And MouseButtons.Left) = MouseButtons.Left Then
If (dragRect <> Rectangle.Empty _
AndAlso Not dragRect.Contains(e.X, e.Y)) Then
DataGridView1.DoDragDrop(DataGridView1.Rows(fromIndex), _
DragDropEffects.Move)
End If
End If
End Sub
Make sure you have the grids AllowDrop
property set to true. 确保将网格AllowDrop
属性设置为true。
UPDATE: 更新:
Instead of 代替
If dragIndex < 0 Then dragIndex = DataGridView1.RowCount - 1
change to 改成
If dragIndex > -1 Then
'action if not selected in the row header and blank space
else
'return error if selected in the column header and blank space
end if
then a error occurs when you drag a row to the "blank zone", if you don't believe me, you must to try it. 当你将一行拖到“空白区域”时会出现错误,如果你不相信我,你必须尝试一下。
the final code (Only for the part "The drag events") is this: 最终代码(仅用于“拖动事件”部分)是这样的:
Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles DataGridView1.DragDrop
Dim p As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
dragIndex = DataGridView1.HitTest(p.X, p.Y).RowIndex
'Determine if dragindex is valid row index
If dragIndex > -1 Then
If (e.Effect = DragDropEffects.Move) Then
Dim dragRow As DataGridViewRow = CType(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
DataGridView1.Rows.RemoveAt(fromIndex)
DataGridView1.Rows.Insert(dragIndex, dragRow)
'Add this line of code if you want to put selected rows to the rows that change
DataGridView1.Rows(dragIndex).Selected = True
End If
Else 'Do any message here if selected in column header and blank space.
End If
End Sub
Here's a Control without the mentioned bug. 这是一个没有上述错误的控件。
Set AllowUserToOrderRows
and AllowDrop
to True
in the Windows Forms Designer and drag the row headers, not the content. 在Windows窗体设计器AllowDrop
AllowUserToOrderRows
和AllowDrop
设置为True
并拖动行标题,而不是内容。
Imports System.ComponentModel
Public Class BetterDataGridView
Inherits DataGridView
<Category("Behavior"), DefaultValue(False)>
Public Property AllowUserToOrderRows As Boolean = False
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
MyBase.OnMouseDown(e)
Dim hitInfo As HitTestInfo = HitTest(e.X, e.Y)
If AllowUserToOrderRows AndAlso
e.Button = MouseButtons.Left AndAlso
hitInfo.ColumnIndex = -1 AndAlso
ValidRow(hitInfo.RowIndex) Then
DoDragDrop(Rows(hitInfo.RowIndex), DragDropEffects.Move)
End If
End Sub
Protected Overrides Sub OnDragOver(e As DragEventArgs)
MyBase.OnDragOver(e)
Dim dragRow As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
Dim targetIndex As Integer = GetRowIndex(e)
e.Effect = If(ValidRowDragDrop(dragRow, targetIndex),
DragDropEffects.Move,
DragDropEffects.None)
End Sub
Protected Overrides Sub OnDragDrop(e As DragEventArgs)
MyBase.OnDragDrop(e)
Dim dragRow As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
Dim targetIndex As Integer = GetRowIndex(e)
If e.Effect = DragDropEffects.Move AndAlso ValidRowDragDrop(dragRow, targetIndex) Then
EndEdit()
Rows.Remove(dragRow)
Rows.Insert(targetIndex, dragRow)
ClearSelection()
dragRow.Selected = True
End If
End Sub
Protected Function ValidRow(rowIndex As Integer) As Boolean
Return rowIndex >= 0 AndAlso
rowIndex < Rows.Count - If(AllowUserToAddRows, 1, 0)
End Function
Protected Function GetRowIndex(e As DragEventArgs) As Integer
Dim clientPos As Point = PointToClient(New Point(e.X, e.Y))
Return HitTest(clientPos.X, clientPos.Y).RowIndex
End Function
Protected Function ValidRowDragDrop(dragRow As DataGridViewRow, targetIndex As Integer) As Boolean
Return dragRow IsNot Nothing AndAlso
ValidRow(targetIndex) AndAlso
targetIndex <> dragRow.Index AndAlso
Rows.Contains(dragRow)
End Function
End Class
Thank you for everything, code working. 谢谢你的一切,代码工作。 I was getting only one error. 我只得到一个错误。 I solved it. 我解决了
if the datagridview "Enable Editing" is set, you receive an error when you throw line spacing. 如果设置了datagridview“启用编辑”,则在抛出行间距时会收到错误。 You can try. 你可以试试。 I solved it as follows: 我解决了如下:
Private Sub DataGridView1(ByVal sender As Object, ByVal e As DragEventArgs) Handles DataGridView1.DragDrop
Dim p As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
dragIndex = DataGridView1.HitTest(p.X, p.Y).RowIndex
If (e.Effect = DragDropEffects.Move) Then
Dim dragRow As DataGridViewRow = CType(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
If dragIndex = DataGridView1.RowCount - 1 Then '**ADD THIS AREA**
DataGridView1.Rows.RemoveAt(fromIndex)
DataGridView1.Rows.Insert(DataGridView1.RowCount - 1, dragRow)
Else
If dragIndex < 0 Then dragIndex = DataGridView1.RowCount - 2 '**this is important**
DataGridView1.Rows.RemoveAt(fromIndex)
DataGridView1.Rows.Insert(dragIndex, dragRow)
End If
End If
End Sub
Thanks for all other information 感谢所有其他信息
1.5 improvements for the event GridView.DragDrop : 1.5事件GridView.DragDrop的改进:
The first 50% improvement, To avoid the descriped error you can also use 前50%的改进,为避免描述错误,您也可以使用
Private Sub DgvSearchFieldCurrent_DragDrop( _ ByVal sender As Object, ByVal e As DragEventArgs) _ Handles DgvSearchFieldCurrent.DragDrop Dim LclDgv As DataGridView = CType(sender, DataGridView) If dragIndex > -1 AndAlso dragIndex < LclDgv.RowCount -1 Then
Second is to set focus to the current row and the first cell: 第二是将焦点设置为当前行和第一个单元格:
LclDgv.Rows.Insert(dragIndex, dragRow) LclDgv.Rows(fromIndex).Selected = False LclDgv.Rows(dragIndex).Selected = True For Each C As DataGridViewColumn In LclDgv.Columns LclDgv(C.Index, fromIndex).Selected = False Next LclDgv(0, dragIndex).Selected = True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.