|
-
Aug 29th, 2012, 02:28 PM
#1
Thread Starter
Junior Member
Filter DataGrid using Combobox
In a simple form (Form1) I have one combobox (ComboBox1) and one datagrid (DataGridView1).
Combobox1 data source is a Access 2010(.accdb) table (tblCoustomers; CID is the ValueMember, CName is the DisplayMember)
DataGridView1 data source is from the same database (tblInvoice)
tblInvoice have those columns:
InvID, InvNumber, InvDate, CID
When I change the value in the ComboBox , i need that the DataGridView to show only the informations corresponding to combobox ValueMember.
Code:
Private Sub ComboBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.BindingSource1.Filter = String.Format("CID LIKE '{0}%'", ComboBox1.ValueMember)
DataGridView1.Visible = True
End Sub
I have also tried, without success:
Me.BindingSource1.Filter = String.Format("Field1 LIKE '{0}%' OR Field2 LIKE '{0}%'",Me.ComboBox1.ValueMember)
Me.BindingSource1.Filter = String.Format("Name LIKE '%{0}%'", Me.ComboBox1)
Me.BindingSource1.Filter = "CID = " & ComboBox1.ValueMember
Me.BindingSource1.Filter = "CID = '" & ComboBox1.ValueMember & "'"
Me.BindingSource1.Filter = "CID Like '%" & ComboBox1.ValueMember & "%'"
The combobox show the DisplayMembers correctly, the DataGrid show ALL the data, but I can't manage to filter those data.
I use VB 2010
-
Aug 29th, 2012, 03:07 PM
#2
Re: Filter DataGrid using Combobox
So you didn't like the ComboBox's .SelectedItem property and decided to invent your own?
-
Aug 29th, 2012, 11:49 PM
#3
Thread Starter
Junior Member
Re: Filter DataGrid using Combobox
There is not a _SelectedItem" propriety in combobox VB 2010
I 've changed "ComboBox1_SelectedIndexChanged_1" to "ComboBox1_SelectedValueChanged", but the DataGridView1 still show all the data. I need only the filtered data (where Combobox CID = Datagridview CID)
Last edited by Retelist; Aug 29th, 2012 at 11:56 PM.
-
Aug 30th, 2012, 12:25 AM
#4
Re: Filter DataGrid using Combobox
 Originally Posted by dunfiddlin
So you didn't like the ComboBox's .SelectedItem property and decided to invent your own? 
Oops! That's not the right property.
There is a SelectedItem property but it's not what you want in this case. The ValueMember is the name of a column and that doesn't change. What you want is the value from that column that corresponds to the item selected by the user. You get that from the SelectedValue property.
-
Aug 30th, 2012, 01:39 AM
#5
Thread Starter
Junior Member
Re: Filter DataGrid using Combobox
OK, but after I changed the propriety to SelectedValue, it still do not filter...
-
Aug 30th, 2012, 01:51 AM
#6
Re: Filter DataGrid using Combobox
What data type is the CID column? If it's not text then you can't use LIKE and you don't wrap it in single quotes.
-
Aug 30th, 2012, 02:24 AM
#7
Thread Starter
Junior Member
Re: Filter DataGrid using Combobox
CID is Number (Access 2010), CName is Text
I also tried:
Me.BindingSource1.Filter = String.Format("CID = '", ComboBox1.ValueMember)
without success...
Last edited by Retelist; Aug 30th, 2012 at 02:31 AM.
-
Aug 30th, 2012, 02:31 AM
#8
Re: Filter DataGrid using Combobox
Then there's your issue. The Filter property of a BindingSource basically represents a SQL WHERE clause so it must obey the same rules. In SQL you don't wrap numbers in single quotes and you cannot use the LIKE operator or wildcards. You can only numerical operators, e.g. = or <. This is the only example of yours that uses valid syntax:
Code:
Me.BindingSource1.Filter = "CID = " & ComboBox1.ValueMember
although that would have to be changed to use the SelectedValue property.
-
Aug 30th, 2012, 03:05 AM
#9
Thread Starter
Junior Member
Re: Filter DataGrid using Combobox
I have attached the project and database (into a .zip archive)... Please, take a look..
VBTest.zip
-
Aug 30th, 2012, 06:11 AM
#10
Re: Filter DataGrid using Combobox
No need. Already explained what to do.
-
Aug 30th, 2012, 08:42 AM
#11
Thread Starter
Junior Member
Re: Filter DataGrid using Combobox
I did that, without success....
my code:
First attempt:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'VBTestDataSet.Customer' table. You can move, or remove it, as needed.
Me.CustomerTableAdapter.Fill(Me.VBTestDataSet.Customer)
'TODO: This line of code loads data into the 'VBTestDataSet.Invoices' table. You can move, or remove it, as needed.
Me.InvoicesTableAdapter.Fill(Me.VBTestDataSet.Invoices)
Me.DataGridView1.Visible = False
End Sub
Private Sub ComboBox1_ValueMember(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.BindingSource1.Filter = "CID =" & ComboBox1.ValueMember
Me.DataGridView1.Visible = True
End Sub
End Class
Nothing happened... no filtering
Second Attempt:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'VBTestDataSet.Customer' table. You can move, or remove it, as needed.
Me.CustomerTableAdapter.Fill(Me.VBTestDataSet.Customer)
'TODO: This line of code loads data into the 'VBTestDataSet.Invoices' table. You can move, or remove it, as needed.
Me.InvoicesTableAdapter.Fill(Me.VBTestDataSet.Invoices)
Me.DataGridView1.Visible = False
End Sub
Private Sub ComboBox1_ValueMemberChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.InvoicesBindingSource.Filter = "CID =" & ComboBox1.ValueMember
Me.DataGridView1.Visible = True
End Sub
End Class
Error: "Syntax error: Missing operand after '=' operator."
I attached the vb project and database...
-
Aug 30th, 2012, 09:41 AM
#12
Thread Starter
Junior Member
Re: Filter DataGrid using Combobox
third attempt:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'VBTestDataSet.Customer' table. You can move, or remove it, as needed.
Me.CustomerTableAdapter.Fill(Me.VBTestDataSet.Customer)
'TODO: This line of code loads data into the 'VBTestDataSet.Invoices' table. You can move, or remove it, as needed.
Me.InvoicesTableAdapter.Fill(Me.VBTestDataSet.Invoices)
Me.DataGridView1.Visible = False
End Sub
Private Sub ComboBox1_SelectedValue(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
Me.InvoicesBindingSource.Filter = "CID =" & ComboBox1.ValueMember
Me.DataGridView1.Visible = True
End Sub
End Class
Error: An error occurred creating the form. See Exception.InnerException for details. The error is: Syntax error: Missing operand after '=' operator.
fourth attempt:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'VBTestDataSet.Customer' table. You can move, or remove it, as needed.
Me.CustomerTableAdapter.Fill(Me.VBTestDataSet.Customer)
'TODO: This line of code loads data into the 'VBTestDataSet.Invoices' table. You can move, or remove it, as needed.
Me.InvoicesTableAdapter.Fill(Me.VBTestDataSet.Invoices)
Me.DataGridView1.Visible = False
End Sub
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
Me.InvoicesBindingSource.Filter = "CID =" & ComboBox1.ValueMember
Me.DataGridView1.Visible = True
End Sub
End Class
Same error as above...
-
Aug 30th, 2012, 10:19 AM
#13
Thread Starter
Junior Member
Re: Filter DataGrid using Combobox
Solveeeeeed!!!!
Working code:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'VBTestDataSet.Customer' table. You can move, or remove it, as needed.
Me.CustomerTableAdapter.Fill(Me.VBTestDataSet.Customer)
'TODO: This line of code loads data into the 'VBTestDataSet.Invoices' table. You can move, or remove it, as needed.
Me.InvoicesTableAdapter.Fill(Me.VBTestDataSet.Invoices)
Me.DataGridView1.Visible = False
End Sub
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
Me.InvoicesBindingSource.Filter = "CID ='" & ComboBox1.SelectedValue & "'"
Me.DataGridView1.Visible = True
End Sub
End Class
Thanks jmcilhinney !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|