日期:2014-05-18 浏览次数:21070 次
//先检查是否选中过 if (ListView1.SelectedIndices.Count==0) return; //记住之前选择 int last = ListView1.SelectedIndices[0]; //删除选中项 ListView1.Items.RemoveAt(last); last = Math.Min(last, ListView1.Items.Count - 1);//如果是最后一条,则减少1个 //查看是否在有效范围 if (ListView1.Items.Count > last)//确认last有效 { ListView1.Items[last].Selected = true;//选中 ListView1.Items[last].Focused = true; //焦点 ListView1.Items[last].EnsureVisible();//滚动显示 }
------解决方案--------------------
先设置listview 必须为单选 (MultiSelect 多选属性),
然后在进行选中行删除以后选中其它行操作,
if (ListView1.SelectedIndices.Count==0) return;
int index = ListView1.SelectedIndices[0];
//删除选中项
ListView1.Items.RemoveAt(index);
if(ListView1.Items.Count==index)
{
ListView1.Items[index-1].Selected = true;//选中
ListView1.Items[index-1].Focused = true; //焦点
ListView1.Items[index-1].EnsureVisible();//滚动显示
}
else if(ListView1.Items.Count>index)
{
ListView1.Items[index].Selected = true;//选中
ListView1.Items[index].Focused = true; //焦点
ListView1.Items[index].EnsureVisible();//滚动显示
}
------解决方案--------------------