如何更改listview列的顺序

5
如何使用C#更改listview列的顺序?最后一列必须是第一列,第一列必须是最后一列。
I have a listview like this

name   age  school  phone      job
----   ---  ------  -----      ---
helen  18    KTU     5224511   student
sarah  25    hitan   2548441   engineer
david  19    havai   2563654   student


I want to make this like that

name   job       phone    school   age
----   ---       -----    -----    ---
helen  student   5224511   KTU     18
sarah  engineer  2548441   hitan   25
david  student   2563654   havai   19

1
这是ListView还是GridView? - Rohit
我正在使用这个listView1.View = View.Details; - nazim corakli
2个回答

5
看起来你只需要按照所需的显示顺序设置(重新排列)列:
  // 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");

最后,如果你想重新排列SubItems(移动数据,而不仅仅是视图更改),你可以像这样做:

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();
}

displayindex只是改变了列表的视图,但我也想改变项目。例如,在使用MessageBox.Show(listview1.Items[0].SubItems[1].Text)之前,代码返回“18”。但在你的代码之后,它再次返回“18”。但它必须返回“student”。 - nazim corakli
@nazim corakli:在这种情况下(如果您想更改子项,而不仅仅是它们的显示顺序),您必须移动(删除/插入)SubItems和Columns(请参见我的编辑)。 - Dmitry Bychenko

2

您只需要更改列索引值即可。(从0开始)。

一个简单的代码示例如下:

        ColumnHeader columnHeader = listView1.Columns[0];
        columnHeader.DisplayIndex = 3;

它只改变了列表的视图。我也想列出项目。 - nazim corakli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接