Filtering a dataview with multiple criteria using Datatimepicker, checkboxes, etc.

This seemed like a fun thing to do but joining the statements got a little confusing since with more than one filter needs an "and" between the filter criteria. I know of one way to do it but it seems like "bad coding" so I want to do it the proper way.


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        ' Start out clean with no filter.
        dv7RowFilter = ""
        If cbOneOn.Enabled = True Then
            dv7.RowFilter = "Date1 " & cmbOne.Text & " #" & dtOne.Value & "#"
        End If
        If cbTwoOn.Checked = True Then
            dv7.RowFilter = dv.RowFilter.ToString & " AND " & ("Date2 " & cmbTwo.Text & " #" & dtTwo.Value & "#")
        End If
        If cbThreeOn.Checked = True Then
            dv7.RowFilter = dv.RowFilter.ToString & " AND " & ("Date3 " & cmbThree.Text & " #" & dtThree.Value & "#")
        End If
End Sub

This code is retarded and broken. Basically it relies on the first box to be checked. I know another way of doing it but it is archaic and I will not post it here. Basically the form would have one datagrid, three check boxes to activate the datetimepickers, the cmdone,cmdtwo,cmdthree are three combo boxed with '<>=' in there collection. Then the submit button is pushed they should run the rowfilter. I am sure someone in the past has come up with a nifty way to filter down a datagrid with date time boxes, checkboxed, etc.
reefcrazedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bchoorCommented:
Try this
        Dim arrList As New ArrayList

        If cbOneOn.Enabled Then
            arrList.Add("Date1 " & cmbOne.Text & " #" & dtOne.Value & "#")
        End If

        If 2 = 2 Then
            arrList.Add("Date2 " & cmbTwo.Text & " #" & dtTwo.Value & "#")
        End If

        If 3 = 3 Then
            arrList.Add("Date3 " & cmbThree.Text & " #" & dtThree.Value & "#")
        End If

        dv7.RowFilter = String.Join(" AND ", arrList.ToArray(GetType(String)))

Basically, you are creating an arraylist and adding each criteria to that arraylist. You are then turning the arraylist into an array of string. You are then joining each item in that array with an " AND " separator. With this implementation if all your conditions are false then RowFilter will be equal to ""

HTH
~BC
0
bchoorCommented:
oh,

 change 2=2 and 3=3, I was just using that to test.
0
tolgaongCommented:
I think the below code will run fuster.
You use too many string concetenations and because string variables are immutable it will take more time comparing to using stringbuilder.


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        ' Start out clean with no filter.
        dim RW as new System.Text.StringBuilder
       
        If cbOneOn.Enabled = True Then
            RW.Append("Date1 ")
            RW.Append(cmbOne.Text)
            RW.Append(" #" )
            RW.Append(dtOne.Value )
            RW.Append("#")
        End If

        If cbOneOn.Enabled = True Then
            if RW.length <> 0 then
                RW.Append(" AND ")
            end if
            RW.Append("Date2 ")
            RW.Append(cmbTwo.Text )
            RW.Append(" #" )
            RW.Append(dtTwo.Value  )
            RW.Append("#")
        End If

        If cbThreeOn.Checked = True Then
            if RW.length <> 0 then
                RW.Append(" AND ")
            end if
            RW.Append("Date3 ")
            RW.Append(cmbThree.Text )
            RW.Append(" #" )
            RW.Append(dtThree.Value  )
            RW.Append("#")
        End If

        dv7RowFilter = RW.ToString()
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

bchoorCommented:
If you're worrying about string concatenations being slow - you can try this as well

        Dim arrList As New ArrayList

        If cbOneOn.Enabled Then
            arrList.Add((New System.Text.StringBuilder).AppendFormat("Date1 {0} #{1}#", cmbOne.Text, dtOne.Value).ToString())
        End If

        If cbTwoOn.Enabled Then
            arrList.Add((New System.Text.StringBuilder).AppendFormat("Date2 {0} #{1}#", cmbTwo.Text, dtTwo.Value).ToString())
        End If

        If cbThreeOn.Enabled Then
            arrList.Add((New System.Text.StringBuilder).AppendFormat("Date3 {0} #{1}#", cmbThree.Text, dtThree.Value).ToString())
        End If

        dv7.RowFilter = String.Join(" AND ", arrList.ToArray(GetType(String)))

HTH
~BC
0
bchoorCommented:
or you could just use
String.Format("Date1 {0} #{1}#", cmbOne.Text, dtOne.Value) for each individual criteria
0
reefcrazedAuthor Commented:
tolgaong idea worked perfectly for my needs. Nothing wrong with your idea bchoor, just not what I was looking for.
0
bchoorCommented:
huh!!! that's cool though, glad you got it figured out.... BC
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.