Link to home
Start Free TrialLog in
Avatar of Ghanisen
Ghanisen

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RonaldBiemans
RonaldBiemans

sorry forgot one line

dv.rowfilter = strfilter
Avatar of Ghanisen

ASKER

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"