Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Filtering a DataView with a combination of criteria

Hi,

I have a Windows with
- a DataGrid (dgr) bound to a DataView (dv),
- 5 ComboBoxes cboVehicle, cboDriver, cboProject, cboUtilisation and cboUser
- 2 DateTimePickers (dtp1 and dtp2) that give the minimum and maximum date to filter the "DateDep" field in the data

dv.RowFilter = "Vehicle_ID = '" & cboVehicle.SelectedValue & "'" works perfectly to filter with a single combo and
dv.RowFilter = "DateDep >= '" & dtp1.Text & "'" and dv.RowFilter = "DateDep <= '" & dtp2.Text & "'" work also separately.

My questions are:

1. How combine (including syntax) several (including all) criteria in the RowFilter method or any other method to filter my data with;
2. How to deal with the event that 1, several or all the ComBoxes may be blank (Text property = " ")  (Remark " " not "")

Any help shall be greatly appreciated.
0
Ghanisen
Asked:
Ghanisen
  • 2
1 Solution
 
RonaldBiemansCommented:
1. dv.RowFilter = "DateDep >= '" & dtp1.Text & "'"  & " AND DateDep <= '" & dtp2.Text & "'" & " AND Vehicle_ID = '" & cboVehicle.SelectedValue & "'" etc..

2. I'm not quite sure what you mean, but if you mean to ignore the ones that are " " then do someting like this

dim StrFilter as string

if cbodriver.selectedvalue <> "" then
    strfilter = "driver_id = '" & cbodirver.selectvalue & "' AND "
end if
if cbovehicle.selectvalue <> " " then
   strfilter += "vehicle_id = '" & cbovehicle.selectedvalue "' AND "
end if
etc....

if strfilter.length > 1 then
            StrFilter = StrFilter.Substring(0, StrFilter.Length - 5)
end if
0
 
RonaldBiemansCommented:
sorry forgot one line

dv.rowfilter = strfilter
0
 
GhanisenAuthor Commented:
Hi Ronald

Thanks very much for your help which got me very close to the final solution which I post for those who might be interested:

Dim strFilter As String

        If cboVehicule.Text <> "" Then
            strFilter = "Vehicle_ID = '" & cboVehicle.SelectedValue & "' AND "
        End If

        If cboDriver.Text <> "" Then
            strFilter += "Driver_ID = '" & cboDriver.SelectedValue & "' AND "
        End If

        If cboProject.Text <> "" Then
            strFilter += "Project_ID = '" & cboProject.SelectedValue & "' AND "
        End If

        If cboUtilisation.Text <> "" Then
            strFilter += "Utilisation = '" & cboUtilisation.Text & "' AND "
        End If

        If cboUser.Text <> "" Then
            strFilter += "User_ID = '" & cboUser.SelectedValue & "' AND "
        End If

        strFilter += "DateDep >= '" & dtp1.Text & "' AND "

        strFilter += "DateDep <= '" & dtp2.Text & "''"

        If strFilter.Length > 1 Then
            strFilter = strFilter.Substring(0, strFilter.Length - 1)
        End If

        dvTrajet.RowFilter = strFilter

        'afficher le nombre d'enregistrements du DataGrid dand le label "lblCount"
        lblCount.Text = "" & dgr.VisibleRowCount & "  enregistrements affich├ęs"
0

Featured Post

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!

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