Saturday, January 16, 2010

Filtering data using VB.NET's DataGridView control...?

I am using a datagridview control in my project. The user can specify up to three separate filter criteria. The program will return the search display these results into the datagridview in one run. My approach is to filter using the databindingsource.filter() method. The program performs the filters in an iterative fashion.





Once the first filter (based upon criteria_1) is completed, the program puts the filtered results in a separate table:





table2 = myTableAdapter.GetData


myBindingSource.DataSource = table





Next, the program automatically processes the second filter based upon criteria_2:





'ComboBox.Text lists column names; TextBox.Text contains search value


myBindingSource.Filter = String.Format(CStr(cboColumnFilter2.Text鈥?%26amp; '; = '; %26amp; CStr(txtFilterValue2.Text))





When I run the program, I receive the following exception:





EvaluateException was unhandled.


Cannot perform '=' operation on System.String and System.Int32.





___Filtering data using VB.NET's DataGridView control...?
If I were you, I would construct another data table to contain the results that is identical to the one you pull down. Then loop through all the rows in your actual data table, copying rows that meet the criteria to the new one. When you're done, databind the new table with filtering criteria applied to the data grid. This approach would probably perform better too.

No comments:

Post a Comment