Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Listview Drag and Drop Rows

I'm trying to implement a C# drag and drop row-reorder with a listview which would then update an SQL database with the current order of the rows. I've come across some snippets of code on the internet (one from this website which implemented a 'var' class) but none seem to be working with my needs. I don't need help updating the database as I have a good idea how I'd do this, but can't seem to get the row reordering to work correctly, any input would be appreciated.

-thanks

m&a

like image 924
Mike Avatar asked Jul 26 '10 13:07

Mike


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

  1. Ensure that AllowDragDrop is set to true.

  2. Implement handlers for at least these 3 events

    private void myList_ItemDrag(object sender, ItemDragEventArgs e)
        {
            DoDragDrop(e.Item, DragDropEffects.Link);
        }
    
        private void myList_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Link;
        }
    
        private void myList_DragDrop(object sender, DragEventArgs e)
        {
            // do whatever you need to reorder the list.
        }
    

    Getting the index of the row you dropped onto may look something like:

    Point cp = myList.PointToClient(new Point(e.X, e.Y));
    ListViewItem dragToItem = myList.GetItemAt(cp.X, cp.Y);
    int dropIndex = dragToItem.Index;
    
like image 65
Matthew Vines Avatar answered Nov 17 '22 14:11

Matthew Vines