Drag Drop Copy entire row - databound DGV to unbound DGV

Ultrawhack

Well-known member
Joined
Jul 5, 2006
Messages
164
Location
Canada
Programming Experience
3-5
I need to drag drop & copy entire row contents from a Source databound datagridview to a Target unbound datagridview.

Source is set to SelectionMode fullrowselect and multiselect is false.

I am not sure how to achieve this and I have not been able to find relevant articles on this.

Thanks for any help !
 
Last edited:
My code using the col.Name as indexer handles this. The code works all right for me (as example without error handling), the DataGridView.DataSource bound to a DataTable of a Dataset. Source datagridview is AutoGenerateColumns=True (default), SelectionMode=FullRowSelect, MultiSelect=False (for example, you could workaround to multiselect=true and send all rows selected).
 
Sure, here is the project.
 

Attachments

  • vbnet20-dragdropDGV.zip
    22.1 KB · Views: 31
Thank you. Your code works perfectly with an xml datasource. Please can you try it with the access table sample attached. I am using an access mdb and for some strange reason the code fails for me with an access datasource. I think vis781 is facing the same issue.

New row is added, columns are cloned but DGV2 cells are empty on dragdrop.

Please can you let me know if you figure out the reason ?
Another issue is this line in DGV1's mousedown fails if user clicks on any headercell in DGV1...
DataGridView1.Rows(hti.RowIndex).Selected = True
error: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
 

Attachments

  • Sample_dgv_2_dgv_row_dragdrop.zip
    291.1 KB · Views: 25
Last edited:
On drop, instead of adding the row then add each column item, do this:
VB.NET:
DataGridView2.Rows.Add(drow.Row.ItemArray)
This works also for reordered columns in your MSAccess bound DGV (but the target DGV columns is not reordered). This variant of code doesn't work properly for my Xml bound DGV, but I haven't had time to investigate this further yet.
 
When you reorder columns you mousedown on column header. The dgv.hittest will return rowindex -1. You will then get a problem with the example code because you can't select row -1. So you should modify it to only select rows with index <> -1 in mousedown event handler. On the same go, you probably only want to handle this when it is the left mouse button that is pressed, the right nouse button is usually only for context menu. You mousedown method will then be something like this:
VB.NET:
Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles DataGridView1.MouseDown
[COLOR=darkgreen]'rule only left mouse button[/COLOR]
If e.Button = Windows.Forms.MouseButtons.Left Then
  Dim hti As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)
  If hti.RowIndex <> -1 Then [COLOR=darkgreen]'rule out column header row[/COLOR]
[COLOR=darkgreen]    'select mousedown row[/COLOR]
    DataGridView1.Rows(hti.RowIndex).Selected = True
[COLOR=darkgreen]    'get data[/COLOR]
    Dim obj As DataRowView = CType(DataGridView1.SelectedRows(0).DataBoundItem, System.Data.DataRowView)
[COLOR=darkgreen]    'start dragdrop[/COLOR]
    DataGridView1.DoDragDrop(obj, DragDropEffects.Copy)
  End If
End If
End Sub
 
Nice catch on the DGV1 mousedown issue. The dragdrop works fine for access bound source DGV but if AllowUserToReorderColumns=true on dgv1 the order of columns on DGV2 remains the same as the field order on the underlying datasource.

On form load the default order of columns in DGV1 will be order of fields on datasource.

* Manually doing this in your databound Source DGV1: going to "Edit Columns" and changing the order of your Selected Columns will cause the code to copy data incorrectly.

So the developer needs to correctly set default order of fields on his datasource.

I'm wondering if there is any way to clone the column order AND data in DGV1 AND allowing for * ?
 
Last edited:
You could AutoGenerateColumns as I said before, it was set False in your project. AllowUserToReorderColumns is not a problem with either two solutions, the col.Name for Xml bound and the ItemArray for MSAcc bound handles these situations. But then again there could be many needs and lots of other solutions, open for anyone in the know..

Maybe you should investigate indexes used for columns? Perhaps you also want to do something with column order in target when user reorders source? I'm sure it's easy to do.
 
JohnH, you had helped me before with the givefeedback and drawcursor code for drag drop between listboxes.

I am having a problem getting it to work for dgvs in this case. Can you please help me out with this ?

Is there any way to choose which column(s) appear as cursor while dragging selected row?
 
It is you who draw the cursor, so it's you who determines what text to draw for cursor image. You should choose some text that is descriptive in relation to the row sent through the dragdrop system.
 
Back
Top