Question Filter multiple columns in Datagridview using a value from textbox

topsykretts

Well-known member
Joined
Dec 22, 2011
Messages
47
Programming Experience
Beginner
Dim textFilter As String
textFilter = TextBox1.Text
Me.BindingSource1.Filter = "FirstColumn like '" & textFilter & "*'"


But what if I wanna compare textFilter with FirstColumn and SecondColumn and ThirdColumn at the same time ????

Tnx in advance
 
The Filter property of a BindingSource basically contains a SQL WHERE clause, just as the Sort property contains a SQL ORDER BY clause. What do you do in the WHERE clause of a SQL query if you want to filter on multiple columns? You add multiple conditions with AND or OR operators between them. The same goes here. I'd also suggest using the String.Format method to build your Filter value, as it will get a bit ugly using using string concatenation to join that many substrings.
 
As you said,I was trying this but doesn't works.

Me.BindingSource1.Filter = String.Format("FirstColumn like '{0}*' AND SecondColumn like '{1}*'", textFilter)

So,where is my mistake??
 
That doesn't work because your format string says use the parameters at index 0 and index 1 but you only have one parameter, at index 0. If you want to use the same parameter in multiple places then you use the same index in multiple places.
 
I putt the same index into both places but nothing again :/

Me.BindingSource1.Filter = String.Format("FirstColumn like '{0}*' AND SecondColumn like '{0}*'", textFilter)

I'am beginner on the VB.net field.
Sorry for stupid questions.
 
Back
Top