How could I Drag and Drop DataGridView Rows under each other?

Viewed 53505

I've DataGridView that bound a List<myClass> and i sort it by "Priority" property in "myClass". So I want to drag an "DataGridViewRow" to certain position to change it's "Priority" property.

How could I "Drag & Drop" DataGridView Rows ?. And how to handle this ?.

2 Answers

The Answer from 'Wahid Bitar' helped me a lot. I cannot comment the above, so a small addition: If deleting a row is unwanted as commented by 'neminem': Just add to DragDrop Event just before the lines that finally moves it:

        if (rowIndexOfItemUnderMouseToDrop < 0 )
        {
            return;
        }        
       dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
       dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
Related