ListView.Sorting 属性

定义

获取或设置控件中项的排序顺序。

C#
public System.Windows.Forms.SortOrder Sorting { get; set; }

属性值

SortOrder

SortOrder 值之一。 默认值为 None

例外

指定的值不是 SortOrder 值之一。

示例

下面的代码示例创建一个控件,其中包含为每个项指定的三ListViewItemListView对象和三ListViewItem.ListViewSubItem个对象。 该示例还创建 ColumnHeader 对象以在详细信息视图中显示子项。 在代码示例中还会创建两 ImageList 个对象,以便为 ListViewItem 对象提供图像。 这些 ImageList 对象将添加到 LargeImageList 属性中 SmallImageList 。 此示例在创建 ListView 控件时使用以下属性。

此示例要求你已将代码添加到 Form 该代码,并从窗体上的构造函数或其他方法调用在示例中创建的方法。 该示例还要求名为 MySmallImage1C MySmallImage2``MyLargeImage1MyLargeImage2映像位于驱动器 C 的根目录中。

C#
private void CreateMyListView()
{
    // Create a new ListView control.
    ListView listView1 = new ListView();
    listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

    // Set the view to show details.
    listView1.View = View.Details;
    // Allow the user to edit item text.
    listView1.LabelEdit = true;
    // Allow the user to rearrange columns.
    listView1.AllowColumnReorder = true;
    // Display check boxes.
    listView1.CheckBoxes = true;
    // Select the item and subitems when selection is made.
    listView1.FullRowSelect = true;
    // Display grid lines.
    listView1.GridLines = true;
    // Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending;
                
    // Create three items and three sets of subitems for each item.
    ListViewItem item1 = new ListViewItem("item1",0);
    // Place a check mark next to the item.
    item1.Checked = true;
    item1.SubItems.Add("1");
    item1.SubItems.Add("2");
    item1.SubItems.Add("3");
    ListViewItem item2 = new ListViewItem("item2",1);
    item2.SubItems.Add("4");
    item2.SubItems.Add("5");
    item2.SubItems.Add("6");
    ListViewItem item3 = new ListViewItem("item3",0);
    // Place a check mark next to the item.
    item3.Checked = true;
    item3.SubItems.Add("7");
    item3.SubItems.Add("8");
    item3.SubItems.Add("9");

    // Create columns for the items and subitems.
    // Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

    //Add the items to the ListView.
    listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

    // Create two ImageList objects.
    ImageList imageListSmall = new ImageList();
    ImageList imageListLarge = new ImageList();

    // Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

    //Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge;
    listView1.SmallImageList = imageListSmall;

    // Add the ListView to the control collection.
    this.Controls.Add(listView1);
}

注解

Sorting 属性允许指定控件中 ListView 是否对项进行排序。 默认情况下,不会执行排序。 Sorting当属性设置为Ascending或设置为或Descending,当属性设置为Ascending) 或降序时,属性设置为) 时Descending,按升 (序按字母顺序 (自动对属性中的ListView项进行排序。 可以使用此属性自动对控件中显示的 ListView 项进行排序,以便用户在大量项目可用时更轻松地查找项。

如果要执行自己的项排序,而不是使用 Sorting 属性,请使用该 ListViewItemSorter 属性与 Sort 方法结合使用。

如果属性 ListView.Sorting 设置为非 SortOrder.None 或设置 ListViewItemSorter 属性的值,则添加项时会自动对列表进行排序。 标签文本更改时,项目不会自动排序。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅