airhalynn101 airhalynn101 - 1 month ago 5
Vb.net Question

Filtring/Searching from a DataGridView

I have a

DataGridView
in a form which displays a client list with the columns
ID
,
First Name
,
Last Name
,
Address
. I also included a
TextBox
to perform the search query. I want to filter my
DataGridView
based from the given columns using a single
TextBox
(like a multi data filter/search where I can select from those four columns by typing on a single
TextBox
).

The question is: am I required to create a binding source for here (I populate my
DataGridView
using sql database) or is there a way to create filters without having to add a binding source?

Answer

You don't populate your grid using a sql database. You populate your grid from a SQL Server database suing something else in between, e.g. a SqlDataAdapter and a DataTable.

A BindingSource is supposed to be a one-stop shop for working with bound data so, while you don't have to use one, I would recommend doing so. Whether it can help you filter your data depends on what it's bound to. The BindingSource doesn't actually do the work of filtering itself but rather passes the work on to the underlying IBindingListView implementation if there is one. For instance, if the underlying data source is a DataTable then the RowFilter of its DefaultView will be used. You could set the DefaultView.RowFilter yourself.

If you want to do all the heavy lifting yourself then you could also search the grid yourself and then hide the rows that don't match. Those rows would still exist though, so you'd have to take that into account when using the data in code.

Comments