DragDrop Listview column 1 to Listbox

Ultrawhack

Well-known member
Joined
Jul 5, 2006
Messages
164
Location
Canada
Programming Experience
3-5
Source can be datagrid1 or multicolumn listview1 populated from access db.

Target is Listbox1.

I need to be able to dragdrop source column1 to target.

What would be the best solution in this situation ?

Thanks !
 
Target Listbox only supports single column, so you have to decide what you want to display. You also have to decide if you see it necessary to preserve all the data in row, or if you only need what is displayed in target. The reason you have to decide is because this is what you 'send' when starting the drag operation with the DoDragDrop.

Only sending the string to be displayed is easy, there are tons of examples and articles out there describing how to do dragdrop, it is basically the same for all controls, the very DoDragDrop method is also one inherited from the base class Control.. If you need to keep more data than what is displayed, you must create an custom class. Populating an instance of this class will allow you both to send this instance with DoDragDrop and simply add it to Listbox when drop arrives. The class must override the ToString method to return what is to be displayed.

You say DataGrid, but you probably mean DataGridView (it is new in .Net 2.0 and replaces DataGrid).

I've included a very simplified example using a custom class so you see how that works in code. If course, in real use you must find the row in mousedown and get the row data, here I just create some dummy object that is dragged each time. Two empty controls DataGridView1 and ListBox1 is used on form in this example.
VB.NET:
Expand Collapse Copy
Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles DataGridView1.MouseDown
  Dim cc As New customclass
  cc.id = 1
  cc.name = "the name"
  DataGridView1.DoDragDrop(cc, DragDropEffects.Copy)
End Sub
 
Private Sub ListBox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListBox1.DragOver
  If e.Data.GetDataPresent(GetType(customclass)) = True Then
    e.Effect = DragDropEffects.Copy
  End If
End Sub
 
Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListBox1.DragDrop
  If e.Data.GetDataPresent(GetType(customclass)) = True Then
    ListBox1.Items.Add(DirectCast(e.Data.GetData(GetType(customclass)), customclass))
  End If
End Sub
 
Public Class customclass
  Public name As String
  Public id As Integer
 
  Public Overrides Function ToString() As String
    Return id.ToString & ". " & name
  End Function
End Class
 
DataGridViews are basically used for editing (and displaying) data, ListViews are basically used for displaying data, so it depends on the usage of the control, some design differences, and actually also your habits :)

I just noticed something, you asked for drag column but I thought about row when replying. The principle is still the same, so I hope you found the reply of value nevertheless. For that matter you could just send the index through the drag routine and populate the listbox when that index is dropped, be it row or column data, be it DataGridView or ListView source. In this case it actually is easier to just send the index. There is perhaps no need to create a complex data object (or array or collection) and send it through the dragdrop, it depends on the overall design of the application and what possible other drag operations the target must accept. If several control sources are to be dragged to this listbox then a simple index signal won't suffice, the listbox wouldn't know what source to grab the data of the given index.
 
Actually I meant trap ListView1 or (DataGridView1)selectedRow, Column1 which should contain "apples" or "oranges" - which can be dragged to Listbox2

Doubleclicking on DataGridView1's selected row would ask for confirmation "Would you like to edit this item" and will open the item for editing on a separate form. Once edited, I would return to original form & refresh DataGridview1.

I probably would not allow editing on Datagridview1 itself.
 
Back
Top