I am new to Visual Basic programming and I am having trouble filtering a column in a DataGridView. The code I am using is the following:
Dim
sText As StringsText =
Me.ComboBox_Set.TextList_Screen.Player_InfoDataGridView.DataSource.Filter =
"Make2 = '"&sText&"'"I have seen an example of this that supposedly works, but it's not for a String, but an Integer. When I type this code, an error message states that "Type characeter '&' does not match declared data type 'String'".
If I replace the filter code to: = "Make2='Smith'" it works. But I am trying to have the user select a name from a ComboBox (ComboBox_Set) rather than hardcode the filter. I think the problem is that the ComboBox is a String and all the information I can find in the help menu is that when you have a String, the criteria must be inside single quotes. My question is what code do I need to use to allow the String generated from the ComboBox to filter the Column Make2 If there is any information that I have not provided that would help someone in responding to my question I would be happy to supply it. Thanks.

BindingSource.Filter Problem
Jav74411
You need to build a combined filterstring. I'd posting an example of this.
Are you comboboxes databound Are they set to dropdownlists or are they still combo boxes
Philsky
Take a look at the following post:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=423975&SiteID=1
I had the same issues you're having and it got worked out in the post.
Tony
pstavroulis
Shakalama, I tried that code and the filter worked if I selected an item from either combobox1 or combobox2, but if I selected an item from both comboboxes it would not filter both. For example, if I select 'Smith' from the name combobox it would filter everyone with the name Smith. If I select 'Ohio' from the state combobox it would filter everyone from Ohio. But if I select 'Smith' and 'Ohio' hoping to get all the Smiths from Ohio, I would only get everyone from Ohio.
Also, if none of the comboboxes are selected after I make an initial filter, I can't get the datagridview to revert back to the unfiltered state. I believe there is code to 'removefilter()', but I don't know where to use it in your example should the user not select any of the comboboxes.
Lastly, I'm still trying to find a way for the combobox to not show the first item in the list when it is displayed on the form. I'm sure there is a way to set the property of the combobox not to show the first item, but I can't find it. Thanks.
BorisM
You have it almost correct. The problem is in building your filter string. I assume the column name you want to search is "Make2". Then try something like:
dim strFilter as string = "Make2='" & sText & "'"
yourBindingSource.Filter=strFilter
Step through your code to make sure strFilter evaluates to "Make2='Smith'" at runtime.
Cheers,
--Mable
Muhammad_Afzaal
Thanks shakalama, but I tried that and it didn't work. Here is the code I now have:
Private
Sub Filter_Form_GO_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Filter_Form_GO.Validating If Me.Set_Filter.Checked ThenList_Screen.Player_InfoDataGridView.DataSource.Filter =
"Make2=" + Me.ComboBox_Set.Text Me.Close() ElseList_Screen.Player_InfoDataGridView.DataSource.RemoveFilter()
Me.Close() End If End SubIf I run the program and select 'Jones' from the ComboBox I get the following exception:
Cannot find column [Jones].
Am I missing a single or double quote somewhere Or do I need to do something else Thanks.
Werners
Here, I pulled out the code from the post I referred to and I believe this is what you're looking for.
Me.YourBindingSource.Filter = String.Format("Make2 = '{0}'", ComboBox_Set.Text)
This will allow you to filter the bindingsource based on the text the user entered into the ComboBox_Set control.
Good luck!
Robert McArthur
I believe that you want to be able to select and combobox or combination of combobox to build up a filter string to use.
The following code is a simple example of what I believe you are looking for. It will search through the controls on the form looking for the combobox control and if the text property is set to something then it will use the tag property which I have used here to hold the field name and the text property of the control to add to a filter string.
So you get a filter string that comprises of all the currently selected comboboxes.
At the moment I have a button which you click to see the contents and would personally have a refresh button - so the user could select whatever combination they want and then when they are ready click refresh which would set the filter string. This way you avoid having things refreshing and filter every time they touch any combobox. I'd only want to retrieve data once I've set everything.
But instead of displaying the contents in a messagebox you would set the filter property with this value.
This is a basic idea and if you have other comboboxes on the form you would have to identify which are FilterCombo which you could do by doing a comparison on the name or something like that.
Class form1
Private Sub RepopulateFilterString(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(constructString)
End Sub
Function constructString() As String
Dim strFilterString As String = ""
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is ComboBox Then
If CType(ctrl, ComboBox).Text.Length > 0 Then
strFilterString = strFilterString & ctrl.Tag & "='" & CType(ctrl, ComboBox).Text & "' "
End If
End If
Next
Return strFilterString
End Function
End Class
megamacy
hi,
your program need a method to make it work
select your combobox , open properties , click events icon (the lightening) find validating event double click in the event , it will creat a handler for you on the code view filter your bindingsource that you datagridview bound to it
BindingSource1.filter = "ColumnName = "+ combobox1.selecteditem(or value or text what ever you want)
hope this helps
Goofy78270
tabdalla, thanks! Worked like a charm. Here's a follow-up question:
Say I have 5 columns named
Column1
Column2
Column3
Column4
Column5
and I have 5 ComboBoxes named
ComboBox1
ComboBox2
ComboBox3
ComboBox4
ComboBox5
and I use the filter code
Me.YourBindingSource.Filter=String.Format("Column1='{0}'",ComboBox1.Text)
to filter the column
How do I, using If Then statements, filter the columns for any combination of ComboBoxes that are selected (or not selected). For example, what if the user wants to use just ComboBox2 and 3 to filter Column2 and 3, but not filter Column1, 4, and 5 Is it possible
Also, and this is just a minor visual problem, how do I set-up a ComboBox where the first item in the list does not show up automatically. In other words, when the ComboBox shows up on the form, I want it to be blank until the user either types something in the field or selects the dropdown arrow to reveal the list. Thanks!
Neil Waterworth
hi,
sure it will not work i was talking about your combobox validating not your form validating it will be something like this
Private Sub ComboBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating, ComboBox2.Validating
Dim name As String = CType(sender, ComboBox).Name
Select Case name
Case "ComboBox1" MessageBox.Show("Column1 =" & ComboBox1.Text)
Case "ComboBox2" MessageBox.Show("Column2 =" & ComboBox2.Text)
End Select End Sub
remove hte message box and put your bindingsource.filter
hope this helps