BindingSource.Sort プロパティ

定義

並べ替えに使用する列名と、データ ソースで行を表示するときの並べ替え順序を取得または設定します。

public:
 property System::String ^ Sort { System::String ^ get(); void set(System::String ^ value); };
public string Sort { get; set; }
public string? Sort { get; set; }
member this.Sort : string with get, set
Public Property Sort As String

プロパティ値

列名の後に "ASC" (昇順) または "DESC" (降順) を付けた、大文字と小文字が区別される文字列。 既定値は、null です。

次の例は、 プロパティを使用して を使用して Sort 基本的な並べ替えを実行する方法を DataView示しています。 この例を実行するには、コードを Windows フォームに貼り付け、フォームのコンストラクターまたはLoadイベント処理メソッドから を呼び出PopulateDataViewAndSortします。 フォームでは、 名前空間と System.IO 名前空間をSystem.Xmlインポートする必要があります。

private void PopulateDataViewAndSort()
{
    DataSet set1 = new DataSet();

    // Some xml data to populate the DataSet with.
    string musicXml =
        "<?xml version='1.0' encoding='UTF-8'?>" +
        "<music>" +
        "<recording><artist>Coldplay</artist><cd>X&Y</cd></recording>" +
        "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" +
        "<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" +
        "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" +
        "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" +
        "</music>";

    // Read the xml.
    StringReader reader = new StringReader(musicXml);
    set1.ReadXml(reader);

    // Get a DataView of the table contained in the dataset.
    DataTableCollection tables = set1.Tables;
    DataView view1 = new DataView(tables[0]);

    // Create a DataGridView control and add it to the form.
    DataGridView datagridview1 = new DataGridView();
    datagridview1.AutoGenerateColumns = true;
    this.Controls.Add(datagridview1);

    // Create a BindingSource and set its DataSource property to
    // the DataView.
    BindingSource source1 = new BindingSource();
    source1.DataSource = view1;

    // Set the data source for the DataGridView.
    datagridview1.DataSource = source1;

    source1.Sort = "cd";
}
Private Sub PopulateDataViewAndSort() 
    Dim set1 As New DataSet()
    
    ' Some xml data to populate the DataSet with.
    Dim musicXml As String = "<?xml version='1.0' encoding='UTF-8'?>" & _
        "<music>" & _
        "<recording><artist>Coldplay</artist><cd>X&Y</cd></recording>" & _
        "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" & _
        "<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" & _
        "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" & _
        "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" & _
         "</music>"
    
    ' Read the xml.
    Dim reader As New StringReader(musicXml)
    set1.ReadXml(reader)
    
    ' Get a DataView of the table contained in the dataset.
    Dim tables As DataTableCollection = set1.Tables
    Dim view1 As New DataView(tables(0))
    
    ' Create a DataGridView control and add it to the form.
    Dim datagridview1 As New DataGridView()
    datagridview1.AutoGenerateColumns = True
    Me.Controls.Add(datagridview1)
    
    ' Create a BindingSource and set its DataSource property to
    ' the DataView.
    Dim source1 As New BindingSource()
    source1.DataSource = view1
    
    ' Set the data source for the DataGridView.
    datagridview1.DataSource = source1
    
    source1.Sort = "cd"

End Sub

次の例では、 プロパティを使用して、 を使用して Sort 高度な並べ替えを実行する方法を DataView示します。 この例を実行するには、コードを Windows フォームに貼り付け、フォームのコンストラクターまたはLoadイベント処理メソッドから を呼び出PopulateDataViewAndAdvancedSortします。 フォームでは、 名前空間と System.IO 名前空間をSystem.Xmlインポートする必要があります。

private void PopulateDataViewAndAdvancedSort()
{
    DataSet set1 = new DataSet();

    // Some xml data to populate the DataSet with.
    string musicXml =
        "<?xml version='1.0' encoding='UTF-8'?>" +
        "<music>" +
        "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" +
        "<recording><artist>Coldplay</artist><cd>X&Y</cd></recording>" +
        "<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" +
        "<recording><artist>U2</artist><cd>Joshua Tree</cd></recording>" +
        "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" +
        "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" +
        "</music>";

    // Read the xml.
    StringReader reader = new StringReader(musicXml);
    set1.ReadXml(reader);

    // Get a DataView of the table contained in the dataset.
    DataTableCollection tables = set1.Tables;
    DataView view1 = new DataView(tables[0]);

    // Create a DataGridView control and add it to the form.
    DataGridView datagridview1 = new DataGridView();
    datagridview1.AutoGenerateColumns = true;
    this.Controls.Add(datagridview1);

    // Create a BindingSource and set its DataSource property to
    // the DataView.
    BindingSource source1 = new BindingSource();
    source1.DataSource = view1;

    // Set the data source for the DataGridView.
    datagridview1.DataSource = source1;

    source1.Sort = "artist ASC, cd ASC";
}
Private Sub PopulateDataViewAndAdvancedSort() 
    Dim set1 As New DataSet()
    
    ' Some xml data to populate the DataSet with.
    Dim musicXml As String = "<?xml version='1.0' encoding='UTF-8'?>" & _
        "<music>" & _
        "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" & _
        "<recording><artist>Coldplay</artist><cd>X&Y</cd></recording>" & _
        "<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" & _
        "<recording><artist>U2</artist><cd>Joshua Tree</cd></recording>" & _
        "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" & _
        "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" & _
        "</music>"
    
    ' Read the xml.
    Dim reader As New StringReader(musicXml)
    set1.ReadXml(reader)
    
    ' Get a DataView of the table contained in the dataset.
    Dim tables As DataTableCollection = set1.Tables
    Dim view1 As New DataView(tables(0))
    
    ' Create a DataGridView control and add it to the form.
    Dim datagridview1 As New DataGridView()
    datagridview1.AutoGenerateColumns = True
    Me.Controls.Add(datagridview1)
    
    ' Create a BindingSource and set its DataSource property to
    ' the DataView.
    Dim source1 As New BindingSource()
    source1.DataSource = view1
    
    ' Set the data source for the DataGridView.
    datagridview1.DataSource = source1
    
    source1.Sort = "artist ASC, cd ASC"

注釈

プロパティは Sort 、行の並べ替えに使用される列名と並べ替えの方向を指定する、大文字と小文字を区別する文字列です。 既定では列は昇順で並べ替えられます。 など、 "State, ZipCode DESC"複数の列をコンマで区切ることができます。

並べ替えをサポートするには、基になるリストで または IBindingListView インターフェイスを実装するIBindingList必要があります。 この機能は、 プロパティを使用して SupportsSorting 照会できます。 プロパティtrueが の場合は、複数列のSupportsAdvancedSorting並べ替えを使用できます。

プロパティを Sort 設定すると、その種類に応じて内部リストが変更されます。

内部リストの並べ替えプロパティは、並べ替え文字列が でない null場合にのみ変更されます。 このプロパティのアクセサーは get 、内部リストの並べ替え値を取得しません。代わりに、アクセサー値を set 返します。 プロパティの Sort 値は、データ ソースが変更されたときに保持されます。

適用対象

こちらもご覧ください