Filter and sort data in a .NET Framework Windows Forms application

Note

Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. They are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.

You filter data by setting the Filter property to a string expression that returns the desired records.

You sort data by setting the Sort property to the column name on which you want to sort; append DESC to sort in descending order, or append ASC to sort in ascending order.

Note

If your application does not use BindingSource components, you can filter and sort data by using DataView objects. For more information, see DataViews.

To filter data by using a BindingSource component

  • Set the Filter property to the expression you want to return. For example, the following code returns customers with a CompanyName that starts with "B":

    customersBindingSource.Filter = "CompanyName like 'B'";
    

To sort data by using a BindingSource component

  • Set the Sort property to the column you want to sort on. For example, the following code sorts customers on the CompanyName column in descending order:

    customersBindingSource.Sort = "CompanyName Desc";