I know how to drag drop from listview to datagrid, but , how to do the other way? Thanks.
Printable View
I know how to drag drop from listview to datagrid, but , how to do the other way? Thanks.
The principles of drag and drop are the same no matter what you're doing. Handle the MouseDown event of the control you're dragging from, call the DoDragDrop method and pass the data you want to drag, handle the DragDrop event of the control you're dragging to, get the data and perfrom whatever operation is required.
Yeah, that is the principles. Now I want to drag from grid to listview, I pass a selected row to listview, but I can not get that row in dragdrop event.
Whatever you pass to the first argument of the DoDragDrop method in the MouseDown event handler is what you get back from the e.Data property in the DragDrop event. The following example is a little rough but it works. Assuming that the DataGrid is bound to a DataTable with columns named ID and Name, this will copy a row from the grid to the ListView when dragged:VB Code:
Private Sub DataGrid1_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown Dim hti As DataGrid.HitTestInfo = Me.DataGrid1.HitTest(e.X, e.Y) If hti.Type = DataGrid.HitTestType.Cell Then Me.DataGrid1.DoDragDrop(DirectCast(Me.DataGrid1.DataSource, DataTable).DefaultView(hti.Row), _ DragDropEffects.Copy) End If End Sub Private Sub ListView1_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter If e.AllowedEffect = DragDropEffects.Copy Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub ListView1_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop Dim row As DataRowView = DirectCast(e.Data.GetData(GetType(DataRowView)), DataRowView) Dim item As New ListViewItem(row("ID").ToString()) item.SubItems.Add(CStr(row("Name"))) Me.ListView1.Items.Add(item) End Sub
Thanks so much!!! And I will need to do dragdrop between grids also. Let me try that first, and maybe I have to come back bug you again. :wave:
Here's that same code rewritten slightly to illustrate what I was saying. The 'dataBeingDragged' variables in the MouseDown and DragDrop event handlers refer to the same object. The same data goes into the DodragDrop method and comes out of the e.Data property. As I said, it's exactly the same principle no matter waht controls are involved and no matter what data you're dragging. You just have to alter the way you get the data from e.Data for the format you want it in. Some data can be retrieved in more than one format, while others can't.VB Code:
Private Sub DataGrid1_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown Dim hti As DataGrid.HitTestInfo = Me.DataGrid1.HitTest(e.X, e.Y) If hti.Type = DataGrid.HitTestType.Cell Then 'This is the data that will be returned by e.Data in the DragDrop event handler. Dim dataBeingDragged As DataRowView = DirectCast(Me.DataGrid1.DataSource, DataTable).DefaultView(hti.Row) Me.DataGrid1.DoDragDrop(dataBeingDragged, DragDropEffects.Copy) End If End Sub Private Sub ListView1_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter If e.AllowedEffect = DragDropEffects.Copy Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub ListView1_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop 'This is the data that was passed to the DoDragDrop method in the MouseDown event handler. Dim dataBeingDragged As DataRowView = DirectCast(e.Data.GetData(GetType(DataRowView)), DataRowView) Dim item As New ListViewItem(dataBeingDragged("ID").ToString()) item.SubItems.Add(CStr(dataBeingDragged("Name"))) Me.ListView1.Items.Add(item) End Sub
I'd love to get this code to compile as I have been trying to implement drag and drop from a datagridview to a listview without success. The line of code with the error is the "hti" declaration in the "MouseDown" procedure. I get the following message when the mouse pointer hovers over this section of the code "Me.DataGrid1.HitTest(e.X, e.Y)":
"Value of type 'System.Windows.Forms.DataGridView.HitTestInfo' cannot be converted to 'System.Windows.Forms.DataGrid.HitTestInfo'"
DataGridView1 is bound to a DataTable and contains only two columns, one each for "cust_id" and "cust_name". I guess there must be something else that I've missed, probably something b-i-g...
Private Sub DataGridView1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
Dim hti As DataGrid.HitTestInfo = Me.DataGridView1.HitTest(e.X, e.Y)If hti.Type = DataGrid.HitTestType.Cell ThenDim dataBeingDragged As DataRowView = DirectCast(Me.DataGridView1.DataSource, DataTable).DefaultView(hti.Row)Me.DataGridView1.DoDragDrop(dataBeingDragged, DragDropEffects.Copy)End IfEnd Sub
Private Sub ListView1_DragEnter(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
If e.AllowedEffect = DragDropEffects.Copy Thene.Effect = DragDropEffects.CopyEnd IfEnd Sub
Private Sub ListView1_DragDrop(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
Dim dataBeingDragged As DataRowView = DirectCast(e.Data.GetData(GetType(DataRowView)), DataRowView)Dim item As New ListViewItem(dataBeingDragged("cust_id").ToString())item.SubItems.Add(CStr(dataBeingDragged("cust_name")))End SubMe.ListView1.Items.Add(item)
I would think the error message would speak for itself. The DataGrid and the DataGridView are two different controls, so they use two different HitTestInfo classes. You're using the wrong one.
Well, that certainly is an eye-opener. I wasn't even aware that a "DataGrid" control existed. I had assumed - incorrectly, of course - that the term DataGrid was being used as shorthand for DataGridView. VB 2005 does not seem to have a DataGrid control available in the Toolbox (the DataGridView control is in the Toolbox, but no DataGrid control is available). Perhaps this might make it easier to understand how I've arrived at this point. Would you kindly direct me to the place where the DataGrid control might be found?
What i meant was that you're using the wrong HitTestInfo class, not the wrong control. You SHOULD be using the DataGridView so you SHOULD be using the DataGridView.HitTestInfo class. The problem is here:Quote:
Originally Posted by sea minkey
The HitTest method of the DataGridView class returns a DataGridView.HitTestInfo object. As the error message says, that cannot be assigned to a DataGrid.HitTest variable. You need to declare your variable as the correct type.vb.net Code:
Dim hti As DataGrid.HitTestInfo = Me.DataGridView1.HitTest(e.X, e.Y)
The DataGrid control has existed since the first version of .NET. In .NET 2.0 the DataGridView control was introduced as a replacement. It is superior in almost every way. The DataGrid is not in the Toolbox by default because you're not intended to use it. It still exists mainly for backward-compatibility, although if you do want to use it for some reason you can simply add it to the toolbox yourself, as you can with any component.