DataView.Sort 属性

定义

获取或设置 DataView 的一个或多个排序列和排序顺序。

public:
 property System::String ^ Sort { System::String ^ get(); void set(System::String ^ value); };
public string Sort { get; set; }
[System.Data.DataSysDescription("DataViewSortDescr")]
public string Sort { get; set; }
member this.Sort : string with get, set
[<System.Data.DataSysDescription("DataViewSortDescr")>]
member this.Sort : string with get, set
Public Property Sort As String

属性值

一个字符串,该字符串包含后跟“ASC”(升序)或"DESC"(降序)的列名。 默认情况下,列按升序排序。 可以用逗号分隔多个列。

属性

示例

以下示例指示 DataView 按两列对表进行排序。

using System.Data;
using System;
public class A {
   static void Main(string[] args) {
      DataTable locationTable = new DataTable("Location");
      // Add two columns
      locationTable.Columns.Add("State");
      locationTable.Columns.Add("ZipCode");

      // Add data
      locationTable.Rows.Add("Washington", "98052");
      locationTable.Rows.Add("California", "90001");
      locationTable.Rows.Add("Hawaii", "96807");
      locationTable.Rows.Add("Hawaii", "96801");
      locationTable.AcceptChanges();

      Console.WriteLine("Rows in original order\n State \t\t ZipCode");
      foreach (DataRow row in locationTable.Rows) {
         Console.WriteLine(" {0} \t {1}", row["State"], row["ZipCode"]);
      }

      // Create DataView
      DataView view = new DataView(locationTable);

      // Sort by State and ZipCode column in descending order
      view.Sort = "State ASC, ZipCode ASC";

      Console.WriteLine("\nRows in sorted order\n State \t\t ZipCode");
      foreach (DataRowView row in view) {
         Console.WriteLine(" {0} \t {1}", row["State"], row["ZipCode"]);
      }
   }
}
Imports System.Data
Public Class A
   Public Shared Sub Main(args As String())
      Dim locationTable As New DataTable("Location")
      ' Add two columns
      locationTable.Columns.Add("State")
      locationTable.Columns.Add("ZipCode")

      ' Add data 
      locationTable.Rows.Add("Washington", "98052")
      locationTable.Rows.Add("California", "90001")
      locationTable.Rows.Add("Hawaii", "96807")
      locationTable.Rows.Add("Hawaii", "96801")
      locationTable.AcceptChanges()

      Console.WriteLine("Rows in original order" & vbLf & " State " & vbTab & vbTab & " ZipCode")
      For Each row As DataRow In locationTable.Rows
         Console.WriteLine(" {0} " & vbTab & " {1}", row("State"), row("ZipCode"))
      Next

      ' Create DataView
      Dim view As New DataView(locationTable)

      ' Sort by State and ZipCode column in descending order
      view.Sort = "State ASC, ZipCode ASC"

      Console.WriteLine(vbLf & "Rows in sorted order" & vbLf & " State " & vbTab & vbTab & " ZipCode")
      For Each row As DataRowView In view
         Console.WriteLine(" {0} " & vbTab & " {1}", row("State"), row("ZipCode"))
      Next
   End Sub
End Class

注解

如果未显式指定 的DataView排序条件,则DataRowView中的 对象DataView将基于 中DataTable.RowsDataRowCollection对应DataRow对象的索引进行排序。

有关详细信息,请参阅 DataViews

适用于

另请参阅