BindingSource.Sort Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient ou définit les noms de colonnes utilisés pour le tri et l’ordre de tri pour afficher les lignes de la source de données.
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
Valeur de propriété
Chaîne sensible à la casse contenant le nom de colonne suivi de « ASC » (pour l’ordre croissant) ou « DESC » (pour la décroissante). La valeur par défaut est null.
Exemples
L’exemple suivant montre comment utiliser la Sort propriété pour effectuer un tri de base avec un DataView. Pour exécuter cet exemple, collez le code dans un Windows Form et appelez PopulateDataViewAndSort à partir du constructeur ou Load de la méthode de gestion des événements du formulaire. Votre formulaire doit importer les espaces de noms et System.IO les System.Xml espaces de noms.
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
L’exemple suivant montre comment utiliser la Sort propriété pour effectuer un tri avancé avec un DataView. Pour exécuter cet exemple, collez le code dans un Windows Form et appelez PopulateDataViewAndAdvancedSort à partir du constructeur ou Load de la méthode de gestion des événements du formulaire. Votre formulaire doit importer les espaces de noms et System.IO les System.Xml espaces de noms.
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"
Remarques
La Sort propriété est une chaîne sensible à la casse qui spécifie les noms de colonnes utilisés pour trier les lignes, ainsi que le sens de tri. Les colonnes sont triées par défaut par ordre croissant. Plusieurs colonnes peuvent être séparées par des virgules, telles que "State, ZipCode DESC".
Pour prendre en charge le tri, la liste sous-jacente doit implémenter les interfaces ou IBindingListView les IBindingList interfaces. Cette fonctionnalité peut être interrogée par le biais de la SupportsSorting propriété. Le tri multicolumn est disponible lorsque la SupportsAdvancedSorting propriété est true.
La définition de la Sort propriété modifie la liste interne en fonction de son type :
Si la liste est de typeIBindingList, les propriétés et IBindingList.SortDirection les IBindingList.SortProperty propriétés sont définies dans la liste interne.
Si la liste est de type IBindingListView, la IBindingListView.SortDescriptions propriété est définie.
Les propriétés de tri de la liste interne ne sont modifiées que lorsque la chaîne de tri n’est pas null. L’accesseur get de cette propriété ne récupère pas la valeur de tri de la liste interne ; à la place, elle retourne la set valeur d’accesseur. La valeur de la Sort propriété persiste lorsque la source de données change.