您需要在他們的新指數,其存儲選擇的對象,排序,找到對象,並重新選擇了。
的代碼可能看起來像這樣(優化它,你認爲合適):
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);
}
}