Udostępnij za pośrednictwem


Instrukcje: sortowanie i filtrowanie danych ADO.NET za pomocą składnika BindingSource formularzy systemu Windows

Możliwość sortowania i filtrowania kontrolki BindingSource można uwidocznić za pomocą właściwości Sort i Filter. Proste sortowanie można zastosować, gdy bazowe źródło danych jest IBindingListi można zastosować filtrowanie i zaawansowane sortowanie, gdy źródło danych jest IBindingListView. Właściwość Sort wymaga standardowej składni ADO.NET: ciąg reprezentujący nazwę kolumny danych w źródle danych, a następnie ASC lub DESC wskazać, czy lista powinna być sortowana w kolejności rosnącej lub malejącej. Zaawansowane sortowanie lub sortowanie wielu kolumn można ustawić, oddzielając każdą kolumnę separatorem przecinka. Właściwość Filter przyjmuje wyrażenie ciągu.

Uwaga / Notatka

Przechowywanie poufnych informacji, takich jak hasło, w parametrach połączenia może mieć wpływ na bezpieczeństwo aplikacji. Użycie uwierzytelniania systemu Windows (nazywanego również zintegrowanymi zabezpieczeniami) to bezpieczniejszy sposób kontrolowania dostępu do bazy danych. Aby uzyskać więcej informacji, zobacz Ochrona informacji o połączeniu.

Aby filtrować dane za pomocą elementu BindingSource

  • Ustaw właściwość Filter na wyrażenie, które chcesz.

    W poniższym przykładzie kodu, wyrażenie jest nazwą kolumny, po której następuje wartość, jaką chcesz mieć dla kolumny.

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

Aby sortować dane za pomocą elementu BindingSource

  1. Ustaw właściwość Sort na nazwę kolumny, po której następuje ASC lub DESC, aby wskazać kolejność rosnącą lub malejącą.

  2. Rozdziel wiele kolumn przecinkami.

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

Przykład

Poniższy przykładowy kod ładuje dane z tabeli Customers przykładowej bazy danych Northwind do kontrolki DataGridView oraz filtruje i sortuje wyświetlane dane.

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

Kompilowanie kodu

Aby uruchomić ten przykład, wklej kod do formularza zawierającego BindingSource o nazwie BindingSource1 i DataGridView o nazwie dataGridView1. Obsłuż zdarzenie Load dla formularza i wywołaj InitializeSortedFilteredBindingSource w metodzie obsługi zdarzenia ładowania.

Zobacz także