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
,中的DataRowView
DataView
物件會根據 中DataTable.Rows
DataRowCollection
對應的DataRow
索引進行排序。
如需詳細資訊,請參閱 DataViews。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應