views:

269

answers:

2

Hi, 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

A: 
Adam
Hi Adam,We thought ListView allows drag-dropping however besides the property allowdrop = true, we can not find any automagic logic that moves the rows around for us. Which is why it looks like implementing it with events (like the post above) is what is required.The var attribute came into question when we looked at the following post:http://stackoverflow.com/questions/643275/c-drag-drop-in-listview
Mike
@Mike yea I realised when I spotted the other post, hence I struck out the relevant part of my answer. What I didn't realise was that the ListView has a GetItemAt method - encapsulating the hit test nicely for you. Using that, all that was needed was to remove the row and insert it at the given index - 1. Also, in that post is the new var keyword - just means the compiler infers the type for you - syntactic sugar.
Adam
Hi, the ItemDrag method passes in the entire ListView control into the DragDrog method, in which I am unable to grab the specific item which I've started dragging, any suggestions?private void empList_ItemDrag(object sender, ItemDragEventArgs e){ DoDragDrop(e.Item, DragDropEffects.Link); empList.Items.RemoveAt(empList.SelectedIndices[0]);}private void empList_DragDrop(object sender, DragEventArgs e){ Point cp = empList.PointToClient(new Point(e.X, e.Y)); ListViewItem dragToItem = empList.GetItemAt(cp.X, cp.Y); int dropIndex = dragToItem.Index;}
Mike
I think this comment would be best against Matthew Vines' answer.
Adam
+1  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(e.X, e.Y));
ListViewItem dragToItem = myList.GetItemAt(cp.X, cp.Y);
int dropIndex = dragToItem.Index;
Matthew Vines
+1 for the row finding sample.
Adam
Thanks, it looks like the method myList_DragDrop will require some tinkering.So to my understanding we're going to have to remove the selected item from the list, and later re-add it to the new index (where we want to drop it?). Plus, we'd also have to shift down the row that we drop into by one...is there an event that handles this?
Mike
Hi, the ItemDrag method passes in the entire ListView control into the DragDrog method, in which I am unable to grab the specific item which I've started dragging, any suggestions? private void empList_ItemDrag(object sender, ItemDragEventArgs e) { DoDragDrop(e.Item, DragDropEffects.Link); empList.Items.RemoveAt(empList.SelectedIndices[0]); } private void empList_DragDrop(object sender, DragEventArgs e) { Point cp = empList.PointToClient(new Point(e.X, e.Y)); ListViewItem dragToItem = empList.GetItemAt(cp.X, cp.Y); int dropIndex = dragToItem.Index; }
Mike