More than once I have come across a form in which sampling data with a DataGridView, but the data show too many, then it is good to add a filter to minimize the number of records displayed.
To do this there are 2 options, the slower it would be a burden on data from the base by applying filters and faster as it operates in memory is to filter the data that are already showing on screen. To do this, in this example will use a DataTable with data from students who dynamic filters.
To begin with this example will need a Form that has a CheckBox to indicate whether to apply the filter or not, a ComboBox to select the field to filter, for entering the TextBox a value of the filter and a DataGrid to display data.
The first thing we do is fill the ComboBox with the name of the columns of our DataTable:
Then add a method that will handle 3 events: CheckBox.CheckedChanged, ComboBox.SelectedIndexChanged and TextBox.TextChanged. In the latter method was chosen to implement the filter or show all the records again:
Now we subtract filter, which will in this case, as the example is simple , will check if the data type for the selected column is String use as a condition LIKE and add a wildcard (*) at the end of the value, if change is Integer = use and check the numeric value is entered.
Also, you may be interested:
How to filter a list of items with LINQ and view the properties for Reflection (VB.Net)
Extension methods in VB.NET (Extension Methods)