Condividi tramite


Procedura: Ordinare e filtrare i dati ADO.NET con il componente BindingSource di Windows Forms

È possibile esporre la funzionalità di ordinamento e filtro del controllo BindingSource tramite le proprietà Sort e Filter. È possibile applicare un ordinamento semplice quando l'origine dati sottostante è un IBindingListed è possibile applicare filtri e ordinamento avanzato quando l'origine dati è un IBindingListView. La proprietà Sort richiede la sintassi standard di ADO.NET: una stringa che rappresenta il nome di una colonna di dati nell'origine dati, seguita da ASC o DESC per indicare se l'elenco deve essere ordinato in ordine crescente o decrescente. È possibile impostare l'ordinamento avanzato o l'ordinamento a più colonne separando ogni colonna con un separatore di virgole. La proprietà Filter accetta un'espressione stringa.

Annotazioni

L'archiviazione di informazioni riservate, ad esempio una password, all'interno della stringa di connessione può influire sulla sicurezza dell'applicazione. L'uso dell'autenticazione di Windows (noto anche come sicurezza integrata) è un modo più sicuro per controllare l'accesso a un database. Per altre informazioni, vedere Protezione delle informazioni di connessione.

Per filtrare i dati con BindingSource

  • Imposta la proprietà Filter sull'espressione desiderata.

    Nell'esempio di codice seguente, l'espressione è un nome di colonna seguito dal valore desiderato per la colonna.

BindingSource1.Filter = "ContactTitle='Owner'";
BindingSource1.Filter = "ContactTitle='Owner'"

Per ordinare i dati con BindingSource

  1. Impostare la proprietà Sort sul nome della colonna desiderata, seguito da ASC o DESC per indicare l'ordine crescente o decrescente.

  2. Separare più colonne con una virgola.

BindingSource1.Sort = "Country DESC, Address ASC";
BindingSource1.Sort = "Country DESC, Address ASC"

Esempio

L'esempio di codice seguente carica i dati dalla tabella Customers del database di esempio Northwind in un controllo DataGridView e filtra e ordina i dati visualizzati.

private void InitializeSortedFilteredBindingSource()
{
    // Create the connection string, data adapter and data table.
    SqlConnection connectionString =
         new SqlConnection("Initial Catalog=Northwind;" +
         "Data Source=localhost;Integrated Security=SSPI;");
    SqlDataAdapter customersTableAdapter =
        new SqlDataAdapter("Select * from Customers", connectionString);
    DataTable customerTable = new DataTable();

    // Fill the adapter with the contents of the customer table.
    customersTableAdapter.Fill(customerTable);

    // Set data source for BindingSource1.
    BindingSource1.DataSource = customerTable;

    // Filter the items to show contacts who are owners.
    BindingSource1.Filter = "ContactTitle='Owner'";

    // Sort the items on the company name in descending order.
    BindingSource1.Sort = "Country DESC, Address ASC";

    // Set the data source for dataGridView1 to BindingSource1.
    dataGridView1.DataSource = BindingSource1;
}
Private Sub InitializeSortedFilteredBindingSource()

    ' Create the connection string, data adapter and data table.
    Dim connectionString As New SqlConnection("Initial Catalog=Northwind;" & _
        "Data Source=localhost;Integrated Security=SSPI;")
    Dim customersTableAdapter As New SqlDataAdapter("Select * from Customers", _
        connectionString)
    Dim customerTable As New DataTable()

    ' Fill the adapter with the contents of the customer table.
    customersTableAdapter.Fill(customerTable)

    ' Set data source for BindingSource1.
    BindingSource1.DataSource = customerTable

    ' Filter the items to show contacts who are owners.
    BindingSource1.Filter = "ContactTitle='Owner'"
    ' Sort the items on the company name in descending order.
    BindingSource1.Sort = "Country DESC, Address ASC"

    ' Set the data source for dataGridView1 to BindingSource1.
    dataGridView1.DataSource = BindingSource1

   
End Sub

Compilazione del codice

Per eseguire questo esempio, incollare il codice in un modulo contenente un BindingSource denominato BindingSource1 e un DataGridView denominato dataGridView1. Gestire l'evento Load per il modulo e chiamare InitializeSortedFilteredBindingSource nel metodo del gestore eventi di caricamento.

Vedere anche