【发布时间】:2016-07-02 09:52:54
【问题描述】:
我正在尝试启动并运行我的第一个应用程序,但我在 datagridview 控件中的拖放操作中遇到了困难。
我创建了一个数据网格视图,其中连接了一个数据源。
Public oBodyAssembly As New BindingList(Of BodyComponent)
DataGridView1.DataSource = oBodyAssembly
在这个DataSource中,用户创建了新的对象,这些对象显示在datagridview中。为了让用户更正或更改他添加对象的初始顺序,我想让他们 拖放行 以重新排列对象在 grid 中的位置,并且在数据源中。
我尝试了这个用 C# 编写的示例代码并将其更改为 VB.NET,它的工作原理是我可以确定我拖动的行并确定拖放的位置。 Link to the example code
但是示例中的代码会插入新行并删除旧行。这对我不起作用。删除工作正常,并且该对象也从我的 DataSource 中删除。另一方面,新行的插入不会。
我的数据源是一个BindingList(Of BodyComponent),它只包含从BodyComponent类派生的对象。
我怎样才能让这个操作起作用?我被卡住了..
这是我目前用于拖放操作的代码。
Public oRowIndexMouseDown As Integer
Public oRow As DataGridViewRow
Private Sub BodyAssemblyDrag_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles DataGridView1.MouseDown
If DataGridView1.SelectedRows.Count = 1 Then
If e.Button = MouseButtons.Left Then
oRow = DataGridView1.SelectedRows(0)
oRowIndexMouseDown = DataGridView1.SelectedRows(0).Index
'Debug.Print("Row to move = " & oRowIndexMouseDown)
DataGridView1.DoDragDrop(sender, DragDropEffects.Move)
End If
End If
End Sub
Private Sub BodyAssemblyDrag_dragenter(ByVal sender As Object, ByVal e As DragEventArgs) Handles DataGridView1.DragEnter
If DataGridView1.SelectedRows.Count = 1 Then
e.Effect = DragDropEffects.Move
End If
End Sub
Private Sub BodyAssemblyDrag_dragdrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles DataGridView1.DragDrop
Dim oPoint As Point
oPoint = DataGridView1.PointToClient(New Point(e.X, e.Y))
Dim oRowIndexMouseDrop As Integer
oRowIndexMouseDrop = DataGridView1.HitTest(oPoint.X, oPoint.Y).RowIndex
'Debug.Print("Drop row @ " & oRowIndexMouseDrop)
If Not oRowIndexMouseDrop = oRowIndexMouseDown Then
'DataGridView1.Rows.RemoveAt(oRowIndexMouseDown)
'DataGridView1.Rows.Insert(oRowIndexMouseDrop, oRow)
End If
End Sub
添加:在列表中创建对象的方法。
Public oBodyAssembly As New List(Of BodyComponent)
Private Sub BTN_BODY_ADD_CILINDER_Click(sender As Object, e As EventArgs) Handles BTN_BODY_ADD_CILINDER.Click
' Create a new cylinder and add it into the oBodyAssembly
Dim oCylinder As New Body_Cylinder
oBodyAssembly.Add(oCylinder)
' Set the index number for this cylinder
oCylinder.Index = oBodyAssembly.Count
' Set the component type
oCylinder.Type = BodyComponent.BodyComponentType.Cylinder
End Sub
Private Sub BTN_BODY_ADD_CONE_Click(sender As Object, e As EventArgs) Handles BTN_BODY_ADD_CONE.Click
' Create a new cone and add it into the oBodyAssembly
Dim oCone As New Body_Cone
oBodyAssembly.Add(oCone)
' Set the index number for this cylinder
oCone.Index = oBodyAssembly.Count
' Set the component type
oCone.Type = BodyComponent.BodyComponentType.Cone_reduction
End Sub
类:
Public Class BodyComponent
' Basic properties that are required for all of the bodycompenents
' regardless of the type.
Public Property Index() As Double
Public Property Type() As BodyComponentType
Public Property Height() As Double
Public Property Thickness() As Double
Public Property Elevation() As Double
Private Property Mass() As Double
' Type Enum that defines what kind of body component is created.
Public Enum BodyComponentType
Cylinder = 0001
Cone_reduction = 0002
End Enum End Class
派生对象(圆锥相同)
Public Class Body_Cylinder
' Get the base properties
Inherits BodyComponent
' Set new properties that are only required for cylinders
Public Property Segments() As Integer
Public Property LW_Orientation() As Double End Class
【问题讨论】:
-
您将需要不同的收藏。您无法对 BindingList 进行排序或重新排序。您确定要在鼠标按下时启动 DargDrop 吗?
-
感谢您的快速回复,那您建议什么收藏?我对集合类型不是很熟悉。不应该在鼠标按下时启动拖放操作。它应该向下移动?但是我以前没有做过这样的事情,知道怎么写。
标签: vb.net datagridview drag-and-drop datasource