简体   繁体   中英

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

  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(eX, eY)); ListViewItem dragToItem = myList.GetItemAt(cp.X, cp.Y); int dropIndex = dragToItem.Index;

I know this isn't ListView specific, but I have sample code for implementing row drag and drop out of / into a DataGridView . Some of it is obviously control specific, other code is actually pretty generic (such as deciding when its a drag):

\n

http://adamhouldsworth.blogspot.com/2010/01/datagridview-multiple-row-drag-drop.html

Completely forgot the fact that ListView already allows drag-dropping!

I can also add some theory - when the drop occurs on your control, you will need to hit test on those coordinates (likely a method called HitTest ) on the ListView to see what row was hit, this is the basis for where to insert the row being dragged.

Unrelated - is that var class perhaps the new var keyword in C#?

so anybody solved the problem ? I'm trying something similar, a dragdrop of rows with data between 2 listviews.

Tried this and a bunch of video for about 4 days but always get errors: drag and drop cell from datagridview to another https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.control.dodragdrop?view=netframework-4.8

can anybody help pls ?

也许与原始版本有所不同...我有一个列表视图,其中充满了我的计算机进程,并且我试图将这一过程数据的一行拖到另一个列表视图中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM