Sorting Searching and Filtering Presented Data with a Binding Source
If the data source bound to the binding source implements the IBindingList or IBindingListView interfaces (covered in detail in Chapter 7). then you may be able to sort, search, or filter the data through the binding source. The data source implementation of the IBindingList interface will have to return TRue from the IBindingList.SupportsSorting property in order to sort through the binding source. If it does, you can provide a sorting expression to the Sort property, and the data exposed through the binding source will automatically be sorted. This doesn't require any direct support for sorting in the control(s) to which the binding source is bound. The following example shows setting a sort expression for a binding source bound to a CustomersDataTable.
private void OnBindSortedCustomerGrid(object sender, EventArgs args) {
m_CustomersGrid.DataSource = m_CustomersBindingSource; CustomersTableAdapter adapter □ = new CustomersTableAdapter(); CustomersDataSet.CustomersDataTable customers = adapter.GetData(); m_CustomersBindingSource.DataSource = customers; m_CustomersBindingSource.Sort = "ContactName ASC";
In this code, a grid is bound to a binding source. The binding source is then bound to CustomersDataTable instance returned from a table adapter. The Sort property on the binding source is then set to"ContactName ASC", which will sort the data from the table in ascending order based on the ContactName column. The grid will then display the data as sorted, because it sees the data as it is exposed by the binding source, regardless of the physical ordering in the underlying data table. The syntax for sort criteria is the name of the sort property, followed by ASC for ascending orDESC for descending. If no sort direction is specified, ascending is used as the default.
Data sources can use this advanced form of sorting through the IBindingListView interface. If a data source implements this interface and returns TRue from the IBindingListView.SupportsAdvancedSorting property, then you can pass a more complex sort expression with multiple sort criteria to the Sort property. This lets you sort on multiple columns or properties in the collection. For example, for a CustomersDataTable, you could pass a sort expression of'Region ASC, CompanyName DESC". This would sort first on theRegion column in an ascending order, then rows that had the same value for Region would be sorted by theCompanyName values in a descending order.
To search a data source through a binding source, you can call the Find method on the binding source. This method takes a property name and an object reference. The property name indicates which property on each of the collection's items should be checked, and the object reference contains the value that is being sought in that property. When the first item in the list is found whose property value for the specified property matches the specified value, its index will be returned. For this to work correctly, the underlying data source has to implement IBindingList and should return true from the IBindingList.SupportsSearching property. The following example shows how to use the Find method.
private void SetCurrentltemToSpecificCompany(string companyName) {
int index = m_CustomersBindingSource.Find("CompanyName",companyName);
m_CustomersBindingSource.Position = index;
This code searches the CompanyName property on each of the items in the list maintained by the binding source and seeks the one with a value that matches whatever was passed into this method. Note that there are no assumptions about the type of the underlying data source or its objects here. This method would work equally well for a CustomersDataTable or a custom collection ofCustomer objects, provided that the custom collection properly implements IBindingList with searching support. See Chapter 9 for an example of how to provide this support in your collections.
If the underlying collection implements the IBindingListView interface and returnsTRue from the IBindingListView.SupportsFiltering property, the Filter property on the binding source can be set to a filter expression. When this is done, the data exposed through the binding source will be filtered to only show the matching data. Depending on the capabilities of the data source, this should work similarly to the filtering capabilities of a Data View in ADO.NET (seeAppendix D for more details). The specific syntax and complexity supported in the filter expression is determined by the data source. The filter expression is just passed through the binding source to the data source, and the filtering is left up to that data source, as shown in the following example:
private void ShowGermanCustomers() {
m_CustomersBindingSource. Filter = "Country = 'Germany'";
This code filters the list exposed by the binding source to only those objects whose Country property (or column in the case of a data table) is equal to Germany. Any bound controls will automatically update to display only those items.
Post a comment