|
-
Jan 29th, 2013, 01:21 AM
#1
Thread Starter
PowerPoster
How to drag and drop a datagridviewrow onto a treeview
Hi Guys,
I want to make u user control that contains a treeview. I will dynamically generate them on the form according to an option selected on a combobox. Then I have two datagridviews. One contains Categories(ID, Name) and the other contains Subcategories(ID,Name,CategoryID).
When I drag a row from gridview 1 it must add a node with that category name to the treeview.
When I drag a row from gridview2(subcategory) it must add a child node to the parent node it has been dropped on.
Please help me get going.
-
Jan 29th, 2013, 01:28 AM
#2
Re: How to drag and drop a datagridviewrow onto a treeview
Do you already know the basics of drag & drop or are you starting from scratch?
-
Jan 29th, 2013, 02:36 AM
#3
Thread Starter
PowerPoster
Re: How to drag and drop a datagridviewrow onto a treeview
hi JM,
I've got the basics down. I set the allowdrop property to true for both controls. Here is the treeview usercontrol code
Code:
Private Sub TreeView1_DragDrop(sender As Object, e As DragEventArgs) Handles TreeView1.DragDrop
Dim row As DataRow = TryCast(e.Data.GetData(GetType(DataRow)), DataRow)
If row IsNot Nothing Then
Dim pt As Point
pt = TreeView1.PointToClient(New Point(e.X, e.Y))
Dim DestinationNode As TreeNode
' Get a handle to the node the source node was dragged onto
DestinationNode = TreeView1.GetNodeAt(pt)
If Not DestinationNode Is Nothing Then
TreeView1.Nodes.Add(row.Field(Of Integer)("ID").ToString, row.Field(Of String)("Name"))
End If
'Dim hi As TreeListHitInfo = TreeList1.CalcHitInfo(TreeList1.PointToClient(New Point(e.X, e.Y)))
''If hi Is Nothing Then
'' Return
''End If
'TreeView1. .AppendNode(row, hi.Node)
End If
End Sub
Private Sub TreeView1_DragEnter(sender As Object, e As DragEventArgs) Handles TreeView1.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
here is the gridview code:
Code:
Private Sub gcCategory_MouseDown(sender As Object, e As MouseEventArgs) Handles gcCategory.MouseDown
hitInfo = GridView1.CalcHitInfo(New Point(e.X, e.Y))
End Sub
Private Sub gcCategory_MouseMove(sender As Object, e As MouseEventArgs) Handles gcCategory.MouseMove
If hitInfo Is Nothing Then
Return
End If
If e.Button <> MouseButtons.Left Then
Return
End If
Dim dragRect As Rectangle = _
New Rectangle(New Point(hitInfo.HitPoint.X - SystemInformation.DragSize.Width / 2, _
hitInfo.HitPoint.Y - SystemInformation.DragSize.Height / 2), SystemInformation.DragSize)
If (Not dragRect.Contains(New Point(e.X, e.Y))) Then
Dim data As DataRow = GridView1.GetDataRow(hitInfo.RowHandle)
gcCategory.DoDragDrop(data, DragDropEffects.Copy)
End If
End Sub
I am able to drag and drop onto the treeview. Not sure if my code is the best. I'm stuck on adding sub nodes. Right now I can only add parent nodes
-
Jan 29th, 2013, 02:49 AM
#4
Re: How to drag and drop a datagridviewrow onto a treeview
When you drop, you should first identify the source of the row being dropped. As you're passing a DataRow, you could test its Table property to see whether you should be creating a root node or a child node. If you need to create a child node then you need to determine which node you dropped on and then add to its Nodes collection rather than the tree's Nodes collection.
You should also be handling the DragOver event of the TreeView. If dropping the current row would result in a new root node then you should display the Copy effect no matter where the cursor is. If it would result in a child node then the cursor must be over a root node to display the copy effect, otherwise dropping is no valid. If the cursor is over a child node or no node at all then no effect should be displayed because dropping cannot create a new node.
-
Jan 30th, 2013, 02:47 AM
#5
Thread Starter
PowerPoster
Re: How to drag and drop a datagridviewrow onto a treeview
thanks again. will let you know how I get along
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|