BindingSource.Sort Propriedade

Definição

Obtém ou define os nomes de coluna usados para classificação e a ordem de classificação para exibir as linhas na fonte de dados.

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

Valor da propriedade

Uma cadeia de caracteres que diferencia maiúsculas de minúsculas que contém o nome da coluna seguido por “ASC” (para ordem crescente) ou “DESC” (para ordem decrescente). O padrão é null.

Exemplos

O exemplo a seguir mostra como usar a Sort propriedade para executar a classificação básica com um DataView. Para executar este exemplo, cole o código em um Windows Form e chame PopulateDataViewAndSort do construtor do formulário ou Load do método de manipulação de eventos. Seu formulário deve importar os System.Xml namespaces e System.IO .

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

O exemplo a seguir mostra como usar a Sort propriedade para executar a classificação avançada com um DataView. Para executar este exemplo, cole o código em um Windows Form e chame PopulateDataViewAndAdvancedSort do construtor do formulário ou Load do método de manipulação de eventos. Seu formulário deve importar os System.Xml namespaces e System.IO .

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"

Comentários

A Sort propriedade é uma cadeia de caracteres que diferencia maiúsculas de minúsculas que especifica os nomes de coluna usados para classificar as linhas, juntamente com a direção de classificação. As colunas são classificadas em ordem crescente por padrão. Várias colunas podem ser separadas por vírgulas, como "State, ZipCode DESC".

Para dar suporte à classificação, a lista subjacente deve implementar as IBindingList interfaces ou IBindingListView . Essa funcionalidade pode ser consultada por meio da SupportsSorting propriedade . A classificação de várias colunas está disponível quando a SupportsAdvancedSorting propriedade é true.

Definir a Sort propriedade alterará a lista interna dependendo de seu tipo:

As propriedades de classificação da lista interna só são alteradas quando a cadeia de caracteres de classificação não nullé . O get acessador dessa propriedade não recuperará o valor de classificação da lista interna; em vez disso, retornará o valor do set acessador. O valor da Sort propriedade persistirá quando a fonte de dados for alterada.

Aplica-se a

Confira também