本人就是想实现,在分组的情况下,一个listview里面的数据移到另一个listview的数据项上面时候,可以获取到是移到另一个listview哪个数据项上面。
两个listview, lvPerson,lvFootBath,从lvPerson里面拖数据项到lvFootBath里面数据的项上面,再没有分组之前,将lvPerson的数据项移到lvFootBath的数据项上面,可以定位出来是在lvFootBath的哪一项上面。但是分组之后,lvFootBath.InsertionMark就出问题了,无法定位到正确的位置。求帮助,下面列出代码。
private void lvFootBath_DragOver(object sender, DragEventArgs e)
{
Point targetPoint = lvFootBath.PointToClient(new Point(e.X, e.Y));
int targetIndex = lvFootBath.InsertionMark.NearestIndex(targetPoint);
labelControl3.Text = targetIndex.ToString();
if (targetIndex > -1)
{
Rectangle itemBounds = lvFootBath.GetItemRect(targetIndex);
if (targetPoint.X > itemBounds.Left + 10
&& targetPoint.X < itemBounds.Left + itemBounds.Width - 20
&& targetPoint.Y > itemBounds.Top + 10
&& targetPoint.Y < itemBounds.Top + itemBounds.Height - 20)
{
lvFootBath.Items[targetIndex].BackColor = Color.Red;
//设置预期放置位置
lvFootBath.InsertionMark.Index = targetIndex;
}
else
{
foreach (ListViewItem item in lvFootBath.Items)
{
item.BackColor = Color.White;
}
}
}
else
{
foreach (ListViewItem item in lvFootBath.Items)
{
item.BackColor = Color.White;
}
}
}
private void lvFootBath_DragDrop(object sender, DragEventArgs e)
{
int targetIndex = lvFootBath.InsertionMark.Index;
if (targetIndex == -1) { return; }
ListViewItem draggedItme = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
//处理一些事情
//重置背景色
foreach (ListViewItem item in lvFootBath.Items)
{
item.BackColor = new Color();
}
lvFootBath.InsertionMark.Index = -1;
}
private void lvFootBath_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
}
private void lvFootBath_DragLeave(object sender, EventArgs e)
{
lvFootBath.InsertionMark.Index = -1;
}
private void lvPerson_ItemDrag(object sender, ItemDragEventArgs e)
{
lvPerson.DoDragDrop(e.Item, DragDropEffects.Move);
}
private void lvPerson_DragLeave(object sender, EventArgs e)
{
lvPerson.InsertionMark.Index = -1;
}
----
