您需要在他们的新指数,其存储选择的对象,排序,找到对象,并重新选择了。
的代码可能看起来像这样(优化它,你认为合适):
void listView1_ColumnClick(object sender, ColumnClickEventArgs args)
{
// Store the selected objects
List<MyDataObject> selectedObjects = new List<MyDataObject>();
foreach (int index in listView1.SelectedIndices)
{
selectedObjects.Add(m_MyDataObjectsColl[index]);
}
// Clear all selected indices
listView1.SelectedIndices.Clear();
// Sort the list
SortListView(listView1, args);
// Reselect the objects according to their new indices
foreach (MyDataObject selectedObject in selectedObjects)
{
int index = m_MyDataObjectsColl.FindIndex(
delegate(MyDataObject obj) { return obj == selectedObject; }
);
listView1.SelectedIndices.Add(index);
}
}