Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Access the Data Rows Filtered by the BindingSource.Filter Property

0.00/5 (No votes)
12 Mar 2010 1  
I recently had created a windows forms application in VB.Net2008 and made use of the BindingSource control to bind all the forms controls to the underlying SQL Queried data.I built user functionality on the form to allow the user to further filter (client side filtering ) the original data...
I recently had created a windows forms application in VB.Net2008 and made use of the BindingSource control to bind all the forms controls to the underlying SQL Queried data.

I built user functionality on the form to allow the user to further filter (client side filtering ) the original data without having to re-query the database.

The BindingSource.Filter property acts like a WHERE clause in SQL and depending on the underlying data will support statements such as "Field = 'xyz'" or "Field Not In('a','b','c')"

I was looking for a method of accessing the filtered row set as a data table to make use of the WriteXML function to export the data to file, after many hours reading the documentation and searching the net, it was still not very clear. Putting together snippets from various places I arrived at the following code to meet my needs.

After performing the initial query on the database and then setting the BindingSource.Filter property, the required data can be accessed via BindingSource.List

To expose the filtered data as a standalone data table the following code can be used to obtain the rows from the binding source;

Dim dv as DataView = ctype(BindingSource.List, DataView)
Dim dt as DataTable = dv.ToTable()


All the filtered data rows are now accessible from the new data table.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here