I've got a listbox from which I'm dragging into the ListView. Now I have groups in the ListView so when the item from the ListView is dropped at the point of the listviewgroup it has to add it under that group.
This is the code which handles the drop.
private void lstvPositions_DragDrop(object sender, DragEventArgs e)
{
var group = lstvPositions.GetItemAt(e.X, e.Y);
var item = e.Data.GetData(DataFormats.Text).ToString();
lstvPositions.Items.Add(new ListViewItem {Group = group.Group, Text = item});
}
I didn't find a function that could give the groupitem, so I used GetItemAt from which I also have access to the listviewgroup.
But GetItemAt always returns null.
Am I doing something wrong? Is there a better way to accomplish this?
-
First, I assume you're using a ListView, not a ListBox, as 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(e.X, e.Y)); 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}); }
Gerbrand : Okay that worked. -
Did that solution worked for u?
because if u drop the in blank space in ListView
lstvPositions.GetItemAt(..) will return nothing
Gerbrand : This works good in my app. In my app there can't be blanks dropped. So I don't have this problem.
0 comments:
Post a Comment