Solved

Filtering a DataView with a combination of criteria

Posted on 2004-08-17
3
251 Views
Last Modified: 2010-04-23
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
Comment
Question by:Ghanisen
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 11818461
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11818512
sorry forgot one line

dv.rowfilter = strfilter
0
 

Author Comment

by:Ghanisen
ID: 11825765
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question