Dragging and dropping between ListView : ListView « GUI Windows Form « C# / C Sharp

Dragging and dropping between ListView


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.ListView listView1= new System.Windows.Forms.ListView();
    private System.Windows.Forms.ListView listView2= new System.Windows.Forms.ListView();
    private System.ComponentModel.Container components = null;
    System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("List 2 Item 1");
    System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("List 2 Item 2");
    System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("List 2 Item 3");
    System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("Item 1");
    System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("Item 2");
    System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("Item 3");

    public Form1() {
        this.listView2.AllowDrop = true;
        listViewItem1.UseItemStyleForSubItems = false;
        listViewItem2.UseItemStyleForSubItems = false;
        listViewItem3.UseItemStyleForSubItems = false;
        this.listView2.Items.AddRange(new System.Windows.Forms.ListViewItem[]{
        this.listView2.Location = new System.Drawing.Point(320, 24);
        this.listView2.Size = new System.Drawing.Size(200, 216);
        this.listView2.View = System.Windows.Forms.View.List;
        this.listView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.OnDragDrop);
        this.listView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.OnDragEnter);
        this.listView1.AllowDrop = true;
        listViewItem4.UseItemStyleForSubItems = false;
        listViewItem5.UseItemStyleForSubItems = false;
        listViewItem6.UseItemStyleForSubItems = false;
        this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] { listViewItem4, listViewItem5, listViewItem6 });
        this.listView1.Location = new System.Drawing.Point(40, 24);
        this.listView1.Size = new System.Drawing.Size(200, 216);
        this.listView1.View = System.Windows.Forms.View.List;
        this.listView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.OnItemDrag);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(576, 273);
        this.Controls.AddRange(new System.Windows.Forms.Control[] { this.listView2, this.listView1 });

    static void Main() {
        Application.Run(new Form1());

    private void OnItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e) {
        string s = e.Item.ToString();
        DoDragDrop(s, DragDropEffects.Copy | DragDropEffects.Move);
    private void OnDragEnter(object sender,
         System.Windows.Forms.DragEventArgs e) {
        if (e.Data.GetDataPresent(DataFormats.Text))
            e.Effect = DragDropEffects.Copy;
            e.Effect = DragDropEffects.None;

    private void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
        string typestring = "Type";
        string s = e.Data.GetData(typestring.GetType()).ToString();
        string orig_string = s;
        s = s.Substring(s.IndexOf(":") + 1).Trim();
        s = s.Substring(1, s.Length - 2);


        IEnumerator enumerator = listView1.Items.GetEnumerator();
        int whichIdx = -1;
        int idx = 0;
        while (enumerator.MoveNext()) {
            string s2 = enumerator.Current.ToString();
            if (s2.Equals(orig_string)) {
                whichIdx = idx;

Related examples in the same category

1.Windows Explorer-Like Program: extends ListView
2.ListView Item clicked eventListView Item clicked event
3.Folder Browser based on ListViewFolder Browser based on ListView
4.Set ColumnHeader for ListView
5.Sort a List View by Any ColumnSort a List View by Any Column
6.Add Data to a ListView
7.Extends ListViewItem
8.Use RadioButton to control the ListView display styleUse RadioButton to control the ListView display style
9.Display Directory info in a ListViewDisplay Directory info in a ListView
10.Add ListView column and insert ListView rowsAdd ListView column and insert ListView rows
11.Use ListViewItem to display file information
12.ListView ExampleListView Example
13.ListView Country: image and fontListView Country: image and font
14.Use ListView to display File info: name, size and dateUse ListView to display File info: name, size and date
15.Use ListView to display file name and double click the name to execute that fileUse ListView to display file name and double click the name to execute that file
16.Use ListView to diaplay folder info and double click to enter that directoryUse ListView to diaplay folder info and double click to enter that directory