必要な表示順序で列を設定 (再配置) する必要があるようです:
// Job (2nd column to display)
myListView.Columns[4].DisplayIndex = 1;
// Phone (3d column to display)
myListView.Columns[3].DisplayIndex = 2;
// School (4th)
myListView.Columns[2].DisplayIndex = 3;
// Age (5th)
myListView.Columns[1].DisplayIndex = 4;
もう少し精巧なコードも使用できます。
public static void ArrangeColumns(ListView listView, params String [] order) {
listView.BeginUpdate();
try {
for (int i = 0; i < order.Length; ++i) {
Boolean found = false;
for (int j = 0; j < listView.Columns.Count; ++j) {
if (String.Equals(listView.Columns[j].Text, order[i], StringComparison.Ordinal)) {
listView.Columns[j].DisplayIndex = i;
found = true;
break;
}
}
if (!found)
throw new ArgumentException("Column " + order[i] + " is not found.", "order");
}
}
finally {
listView.EndUpdate();
}
}
...
ArrangeColumns(myListView, "name", "job", "phone", "school", "age");
最後に、SubItem を再配置したい場合 (ビューの変更だけでなく、データを移動する場合) は、次のように実行できます。
public static void SwapSubItems(ListView listView, int fromIndex, int toIndex) {
if (fromIndex == toIndex)
return;
if (fromIndex > toIndex) {
int h = fromIndex;
fromIndex = toIndex;
toIndex = h;
}
listView.BeginUpdate();
try {
foreach (ListViewItem line in listView.Items) {
var subItem = line.SubItems[fromIndex];
line.SubItems.RemoveAt(fromIndex);
line.SubItems.Insert(toIndex, subItem);
if (listView.Columns.Count > toIndex) {
var column = listView.Columns[fromIndex];
listView.Columns.RemoveAt(fromIndex);
listView.Columns.Insert(toIndex, column);
}
}
}
finally {
listView.EndUpdate();
}
}
...
myListView.BeginUpdate();
try {
// Job
SwapSubItems(myListView, 4, 1);
// Phone (now, when Job moved, Phone is a 4th column)
SwapSubItems(myListView, 4, 2);
// School (now, when Job and Phone moved, School is a 4th column)
SwapSubItems(myListView, 4, 3);
}
finally {
myListView.EndUpdate();
}