• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

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.
0
reefcrazed
Asked:
reefcrazed
  • 5
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now