Solved

Filtering a DataView with a combination of criteria

Posted on 2004-08-17
3
253 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending receiving text messages in vb.net 15 62
vb.net 1 month apart 11 38
FInd Image Control Gridview 3 34
Import MySQL data into MS Access using VB.Net interface 5 47
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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