- This topic shows how to Drag and Drop list of Data to List View in C#.
- Tools needed: ListBox1 (ListBox1), ListBox2 (ListBox2).
To move multiple items back and forth between two lists
- Add two List View controls to a form.
- Set the AllowDrop property of each List View control to true.
- Set the MultiSelect property of each List View control to true.
- Set the View property of each List View control to List.
- Add the below code:
Drag and Drop – How to Drag and Drop list of Data to List View in C#
- Drag and Drop is used to move items from one place to another through dragging, it can be moved around by holding the mouse down on them, and that they’ll get appropriate visual feedback when they’re over a spot where the item can be dropped.
- To begin a drag and drop operation, you have to call the DoDragDrop method of a Windows Forms control.
- The DoDragDrop method is implemented on the System.Windows.Forms.Control class, which means that it is available on all controls within the Windows Forms namespace.
How to Drag and Drop list of Data to List View in C# – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace TestC { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void listView1_DragDrop(object sender, DragEventArgs e) { ListViewItem myItem = default(ListViewItem); ListViewItem[] myItems = e.Data.GetData("System.Windows.Forms.ListViewItem()"); int i = 0; foreach ( myItem in myItems) { // Add the item to the target list. sender.Items.Add(myItems(i).Text); // Remove the item from the source list. if (object.ReferenceEquals(sender, ListView1)) { ListView2.Items.Remove(ListView2.SelectedItems.Item(0)); } else { ListView1.Items.Remove(ListView1.SelectedItems.Item(0)); } i = i + 1; } } private void listView1_DragEnter(object sender, DragEventArgs e) { // Check for the custom DataFormat ListViewItem array. { if (e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()")) { e.Effect = DragDropEffects.Move; } else { e.Effect = DragDropEffects.None; } } } private void listView1_ItemDrag(object sender, ItemDragEventArgs e) { ListViewItem myItem = default(ListViewItem); ListViewItem[] myItems = new ListViewItem[sender.SelectedItems.Count]; int i = 0; // Loop though the SelectedItems collection for the source. foreach ( myItem in sender.SelectedItems) { // Add the ListViewItem to the array of ListViewItems. myItems(i) = myItem; i = i + 1; } // Create a DataObject containg the array of ListViewItems. sender.DoDragDrop(new DataObject("System.Windows.Forms.ListViewItem()", myItems), DragDropEffects.Move); } } } |
How to Drag and Drop list of Data to List View in C#






