| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Sort and Filter ADO.NET Data with the Windows Forms BindingSource Component |
03/30/2017 |
.net-framework |
|
article |
|
|
6c206daf-d706-4602-9dbe-435343052063 |
15 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Sort and Filter ADO.NET Data with the Windows Forms BindingSource Component
You can expose the sorting and filtering capability of xref:System.Windows.Forms.BindingSource control through the xref:System.Windows.Forms.BindingSource.Sort%2A and xref:System.Windows.Forms.BindingSource.Filter%2A properties. You can apply simple sorting when the underlying data source is an xref:System.ComponentModel.IBindingList, and you can apply filtering and advanced sorting when the data source is an xref:System.ComponentModel.IBindingListView. The xref:System.Windows.Forms.BindingSource.Sort%2A property requires standard [!INCLUDEvstecado] syntax: a string representing the name of a column of data in the data source followed by ASC or DESC to indicate whether the list should be sorted in ascending or descending order. You can set advanced sorting or multiple-column sorting by separating each column with a comma separator. The xref:System.Windows.Forms.BindingSource.Filter%2A property takes a string expression.
[!NOTE] Storing sensitive information, such as a password, within the connection string can affect the security of your application. Using Windows Authentication (also known as integrated security) is a more secure way to control access to a database. For more information, see Protecting Connection Information.
To filter data with the BindingSource
-
Set the xref:System.Windows.Forms.BindingSource.Filter%2A property to expression that you want.
In the following code example, the expression is a column name followed by value that you want for the column.
[!code-csharpSystem.Windows.Forms.DataConnectorFilterAndSort#11] [!code-vbSystem.Windows.Forms.DataConnectorFilterAndSort#11]
To sort data with the BindingSource
-
Set the xref:System.Windows.Forms.BindingSource.Sort%2A property to the column name that you want followed by
ASCorDESCto indicate the ascending or descending order. -
Separate multiple columns with a comma.
[!code-csharpSystem.Windows.Forms.DataConnectorFilterAndSort#12] [!code-vbSystem.Windows.Forms.DataConnectorFilterAndSort#12]
Example
The following code example loads data from the Customers table of the Northwind sample database into a xref:System.Windows.Forms.DataGridView control, and filters and sorts the displayed data.
[!code-csharpSystem.Windows.Forms.DataConnectorFilterAndSort#1] [!code-vbSystem.Windows.Forms.DataConnectorFilterAndSort#1]
Compiling the Code
To run this example, paste the code into a form that contains a xref:System.Windows.Forms.BindingSource named BindingSource1 and a xref:System.Windows.Forms.DataGridView named dataGridView1. Handle the xref:System.Windows.Forms.Form.Load event for the form and call InitializeSortedFilteredBindingSource in the load event handler method.
See Also
xref:System.Windows.Forms.BindingSource.Sort%2A
xref:System.Windows.Forms.BindingSource.Filter%2A
How to: Install Sample Databases
BindingSource Component