C # Drag and Drop in listview

I have a list from which I drag into a ListView. Now I have groups in the ListView, so when an item from the ListView is dropped at the point of listviewgroup, it should add it to this group.

This is the code that handles the fall.

private void lstvPositions_DragDrop(object sender, DragEventArgs e) { var group = lstvPositions.GetItemAt(eX, eY); var item = e.Data.GetData(DataFormats.Text).ToString(); lstvPositions.Items.Add(new ListViewItem {Group = group.Group, Text = item}); } 

I did not find a function that could give groupitem, so I used GetItemAt, from which I also had access to the listview group.

But GetItemAt always returns null.

Am I doing something wrong? Is there a better way to do this?

0
c # listview drag-and-drop
source share
1 answer

First, I assume that you are using a ListView, not a ListBox, since the ListBox does not contain a GetItemAt member.

To solve your problem, convert the point to local coordinates:

 private void lstvPositions_DragDrop(object sender, DragEventArgs e) { var localPoint = lstvPositions.PointToClient(new Point(eX, eY)); var group = lstvPositions.GetItemAt(localPoint.X, localPoint.Y); var item = e.Data.GetData(DataFormats.Text).ToString(); lstvPositions.Items.Add(new ListViewItem {Group = group.Group, Text = item}); } 
+2
source share

All Articles