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

Możliwość sortowania i filtrowania kontrolki BindingSource można uwidocznić za pomocą Sort właściwości i Filter . Proste sortowanie można zastosować, gdy bazowe źródło danych to IBindingList, i 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 wskazać, czy lista powinna być sortowana w kolejności rosnącej lub DESC 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

Przechowywanie poufnych informacji, takich jak hasło, w parametry połączenia może mieć wpływ na bezpieczeństwo aplikacji. Korzystanie z uwierzytelniania systemu Windows (znanego również jako zabezpieczenia zintegrowane) jest bezpieczniejszym sposobem na kontrolowanie dostępu do bazy danych. Aby uzyskać więcej informacji, zobacz Ochrona informacji o Połączenie ion.

Aby filtrować dane za pomocą elementu BindingSource

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

    W poniższym przykładzie kodu wyrażenie jest nazwą kolumny, po której ma być wybrana wartość kolumny.

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

Aby sortować dane za pomocą elementu BindingSource

  1. Sort Ustaw właściwość na nazwę kolumny, której chcesz ASC użyć, 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 DataGridView kontrolki 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 nazwane BindingSource1 i nazwane DataGridViewdataGridView1. Load Obsłuż zdarzenie formularza i wywołaj InitializeSortedFilteredBindingSource metodę obsługi zdarzeń ładowania.

Zobacz też