DataView.Sort 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定排序數據行或數據行,以及 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
的排序準則,DataView
中的 DataRowView
對象會根據 DataTable.Rows
DataRowCollection
中對應 DataRow
的索引來排序。
如需詳細資訊,請參閱 DataViews。