|
-
Oct 27th, 2009, 01:16 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] DGV Combined Filter
Hi,
I am filtering a Datagridview with combined help of 4 textboxes and 4 comboboxes each dedicated to a field in DB. All i know about this process is i can combine two or more filters but how do i check at a given time, whether which of these (cb & tb) have been used already to perform the filter already and then collect all the instances and perform the collective filter.
Any suggestions??
Lux.
-
Oct 27th, 2009, 08:43 PM
#2
Re: DGV Combined Filter
What values have been used to filter before is irrelevant. Just build the entire filter string each time.
-
Oct 29th, 2009, 03:43 AM
#3
Thread Starter
Hyperactive Member
Re: DGV Combined Filter
Now suppose i have these three filters:
Code:
ClientProfileBindingSource.Filter = String.Format("Convert(MachineID, 'System.String') LIKE '{0}%'", TextBox2.Text)
and
Code:
ClientProfileBindingSource.Filter = "Category = '" & ComboBox2.Text & "'"
and
Code:
ClientProfileBindingSource.Filter = String.Format("ClientType = '{0}' OR ClientType = '{1}'", "Seeker", "Both")
You mean to say that under all of their event change, i should use a single combined filter?
Code:
ClientProfileBindingSource.Filter = String.Format("Convert(MachineID, 'System.String') LIKE '{0}%'", TextBox2.Text) AND "Category = '" & ComboBox2.Text & "'" AND String.Format("ClientType = '{0}' OR ClientType = '{1}'", "Seeker", "Both")
When i am using the above code i am getting error:
Conversion from string "Convert(MachineID, 'System.Strin" to type 'Long' is not valid.
How can it be resolved?
-
Oct 29th, 2009, 04:12 AM
#4
Re: DGV Combined Filter
Check this one
Code:
ClientProfileBindingSource.Filter
=String.Format("(Convert(MachineID, 'System.String')
LIKE '{0}%') AND (Category ='{1}' ) AND
(ClientType = '{2}' OR ClientType = '{3}' ) ",
TextBox2.Text, ComboBox2.Text,"Seeker", "Both")
Please mark you thread resolved using the Thread Tools as shown
-
Oct 29th, 2009, 05:45 AM
#5
Thread Starter
Hyperactive Member
Re: DGV Combined Filter
i used this:
vb Code:
ClientProfileBindingSource.Filter = String.Format("(Convert(MachineID,'System.String') LIKE '{0}%') AND (Convert(FirstName,'System.String') LIKE '{1}%') AND (Category = '{2}') AND (Convert(LastName,'System.String') LIKE '{3}%') AND (Convert(Mobile,'System.String') LIKE '{4}%') AND (Type = '{5}') AND (Brand = '{6}') AND (Model = '{7}') AND (State = '{8}') AND (District = '{9}') AND (City = '{10}') AND (Convert(ClientID,'System.String') LIKE '{11}%') AND (Availability = '{12}')", TextBox2.Text, Cname.Text, ComboBox2.Text, TextBox3.Text, TextBox4.Text, ComboBox4.Text, ComboBox3.Text, ComboBox5.Text, ComboBox1.Text, ComboBox6.Text, ComboBox7.Text, ComboBox8.Text)
and i got the following error:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
-
Oct 29th, 2009, 06:37 AM
#6
Re: DGV Combined Filter
Check the format arguments it is 12
TextBox2.Text, Cname.Text, ComboBox2.Text, TextBox3.Text, TextBox4.Text, ComboBox4.Text, ComboBox3.Text, ComboBox5.Text, ComboBox1.Text, ComboBox6.Text, ComboBox7.Text, ComboBox8.Text
and you are passing only 11
Please mark you thread resolved using the Thread Tools as shown
-
Oct 29th, 2009, 07:44 AM
#7
Thread Starter
Hyperactive Member
Re: DGV Combined Filter
oops.. got it right.
i have placed this code in all the event change method of the controls. But when i enter text in text box or select an item in combobox, i get no result in DGV.
I need to perform this filter mechanism by use of any possible combinations of the controls for which i hope 'AND' is the right operator. Am i wrong?
-
Oct 29th, 2009, 08:43 AM
#8
Re: DGV Combined Filter
 Originally Posted by LuxCoder
oops.. got it right.
i have placed this code in all the event change method of the controls. But when i enter text in text box or select an item in combobox, i get no result in DGV.
I need to perform this filter mechanism by use of any possible combinations of the controls for which i hope 'AND' is the right operator. Am i wrong?
For textBoxes call the the method TextBox.LeaveEvent of the textBox.
And for the Combo Box you have call in the the Combobox _SelectedIndex Changed method.
And for the And only you have decide whether to use AND or OR
Please mark you thread resolved using the Thread Tools as shown
-
Oct 31st, 2009, 05:58 PM
#9
Thread Starter
Hyperactive Member
Re: DGV Combined Filter
Tried what you suggested.
But how does it matter what even it is being used?
Is it not that, that the other blank textboxes and comboboxes are included in the filter when they have no values in them ???
-
Oct 31st, 2009, 07:43 PM
#10
Re: DGV Combined Filter
You've got two choices:
1. If you don't want empty fields added to the filter then don't add them. That's what If statements are for:
vb.net Code:
Dim filters As New List(Of String) If Me.TextBox1.Text.Trim().Length > 0 Then filters.Add(String.Format("Column1 LIKE '%{0}%'", Me.TextBox1.Text.Trim())) End If If Me.TextBox2.Text.Trim().Length > 0 Then filters.Add(String.Format("Column2 LIKE '%{0}%'", Me.TextBox2.Text.Trim())) End If If Me.TextBox3.Text.Trim().Length > 0 Then filters.Add(String.Format("Column3 LIKE '%{0}%'", Me.TextBox3.Text.Trim())) End If Dim filter As String = String.Join(" AND ", filters)
2. Include everything but add extra filters to account for empty fields:
vb.net Code:
Dim filter As String = String.Format("('{0}' = '' OR Column1 = '{0}') AND ('{1}' = '' OR Column2 = '{1}') AND ('{2}' = '' OR Column3 = '{2}')", _ Me.TextBox1.Text.Trim(), _ Me.TextBox2.Text.Trim(), _ Me.TextBox3.Text.Trim())
Note that I use '=' rather than 'LIKE' in that second example because you don't need the extra filters for LIKE. If the field is empty then every record will match "LIKE '%%'" anyway.
-
Nov 1st, 2009, 09:15 AM
#11
Thread Starter
Hyperactive Member
-
Nov 19th, 2009, 12:06 AM
#12
Thread Starter
Hyperactive Member
Re: [RESOLVED] DGV Combined Filter
I am using the following to join the filter string and perform a filter on the DGV. I tried a lot but it gives me error
Error 1 Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to '1-dimensional array of String'.
vb Code:
Dim filters As New List(Of String) If (reader("Category".ToString)).Length > 0 Then filters.Add(String.Format("(Convert(Category, 'System.String') LIKE '%{0}%')", (reader("Category")))) End If If (reader("Type".ToString)).Length > 0 Then filters.Add(String.Format("(Convert(Type, 'System.String') LIKE '%{0}%')", (reader("Type")))) End If Dim filter As String = String.Join(" And ", filters) ClientProfileBindingSource.Filter = filter
what should i do?
-
Nov 19th, 2009, 12:25 AM
#13
Re: [RESOLVED] DGV Combined Filter
I typed that code straight into the forum so didn't have the IDE to check it for me. That said, the fix is fairly simple. You've got a List(Of String) and you need a String(). How do you create an array from a List? You call ToArray on it.
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
|