BindingSource.Sort BindingSource.Sort BindingSource.Sort BindingSource.Sort Property

Définition

Obtient ou définit les noms de colonnes utilisés pour le tri, et l'ordre de tri pour consulter les lignes dans la source de données.Gets or sets the column names used for sorting, and the sort order for viewing the rows in the data source.

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

Valeur de propriété

Chaîne qui respecte la casse contenant le nom de la colonne suivi de "ASC" (ordre croissant) ou de "DESC" (ordre décroissant).A case-sensitive string containing the column name followed by "ASC" (for ascending) or "DESC" (for descending). La valeur par défaut est null.The default is null.

Exemples

L’exemple suivant montre comment utiliser la Sort propriété pour effectuer un tri de base avec un. DataViewThe following example shows how to use the Sort property to perform basic sorting with a DataView. Pour exécuter cet exemple, collez le code dans un Windows Form et PopulateDataViewAndSort appelez à partir du constructeur du Load formulaire ou de la méthode de gestion d’événements.To run this example, paste the code into a Windows Form and call PopulateDataViewAndSort from the form's constructor or Load event-handling method. Votre formulaire doit importer les System.Xml espaces System.IO de noms et.Your form should import the System.Xml and System.IO namespaces.

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&amp;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&amp;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

L’exemple suivant montre comment utiliser la Sort propriété pour effectuer un tri avancé avec un. DataViewThe following example shows how to use the Sort property to perform advanced sorting with a DataView. Pour exécuter cet exemple, collez le code dans un Windows Form et PopulateDataViewAndAdvancedSort appelez à partir du constructeur du Load formulaire ou de la méthode de gestion d’événements.To run this example, paste the code into a Windows Form and call PopulateDataViewAndAdvancedSort from the form's constructor or Load event-handling method. Votre formulaire doit importer les System.Xml espaces System.IO de noms et.Your form should import the System.Xml and System.IO namespaces.

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&amp;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&amp;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"

Remarques

La Sort propriété est une chaîne qui respecte la casse et qui spécifie les noms de colonnes utilisés pour trier les lignes, ainsi que le sens du tri.The Sort property is a case-sensitive string that specifies the column names used to sort the rows, along with the sort direction. Les colonnes sont triées par ordre croissant par défaut.Columns are sorted ascending by default. Plusieurs colonnes peuvent être séparées par des virgules, par "State, ZipCode DESC"exemple.Multiple columns can be separated by commas, such as "State, ZipCode DESC".

Pour prendre en charge le tri, la liste sous IBindingList - IBindingListView jacente doit implémenter les interfaces ou.To support sorting, the underlying list must implement the IBindingList or IBindingListView interfaces. Cette fonctionnalité peut être interrogée par le SupportsSorting biais de la propriété.This capability can be queried through the SupportsSorting property. Le tri multicolonne est disponible lorsque SupportsAdvancedSorting la propriété trueest.Multicolumn sorting is available when the SupportsAdvancedSorting property is true.

La définition Sort de la propriété modifie la liste interne en fonction de son type:Setting the Sort property will change the internal list depending on its type:

Les propriétés de tri de la liste interne sont modifiées uniquement lorsque la chaîne de nulltri n’est pas.The internal list's sort properties are only changed when the sort string is not null. L' get accesseur de cette propriété ne récupère pas la valeur de tri de la liste interne; à la place, set elle retourne la valeur de l’accesseur.The get accessor for this property will not retrieve the sort value of the internal list; instead, it will return the set accessor value. La valeur de la Sort propriété est persistante lorsque la source de données est modifiée.The value of the Sort property will persist when the data source changes.

S’applique à

Voir aussi