gridview filtering: 2 dropdown lists and one calendar control needed

what is the best approach to allow filtering of a gridview.  I want to use 2 dropdown lists and two sets of calendars.  User may choose one or all or a few of them.  What I have so far seems to work for one of the drop down lists but when I add the second one it doesn't seem to filter for it.  Also I am not sure how to add calendars for users to select from.

Under SQLDataSource:

SelectCommand="SELECT * FROM [atable] WHERE (([afield] = @afield) OR ([bfiled] = @bfield))">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="afield" PropertyName="SelectedValue"
                    Type="String" />
                <asp:ControlParameter ControlID="DropDownList2" Name="bfield" PropertyName="SelectedValue"
                    Type="String" />
            </SelectParameters>
 
On page load:
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If DropDownList1.SelectedIndex.Equals(0) And DropDownList2.SelectedIndex.Equals(0) Then
            data.SelectCommand = ("SELECT * FROM [atable]")
            GridView1.DataBind()
 
        End If
    End Sub

Open in new window

LVL 4
newbiealAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GreymanMSCCommented:
If you have a Value="" in the first ListItem of the DropDownLists, you do not need the code to change the select command when one list is unselected.  Just place CancelSelectOnNullParameter="False" into your SQLDataSource, place ConvertEmptyStringToNull="True" into the ControlParameters, and adjust the SelectCommand to account for nullity.


         CancelSelectOnNullParameter="False"
         SelectCommand="SELECT * FROM atable 
            WHERE ((aField = @aField) Or (@aField Is Null))
            AND ((bField = @bField) OR (@bField Is Null))
            AND ((@cField Is Null) OR
              (Convert(varchar(10),cField,102)
              = Convert(varchar(10),@cField,102)))">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1"
                    Name="aField" PropertyName="SelectedValue"
                    ConvertEmptyStringToNull="True" />
                <asp:ControlParameter ControlID="DropDownList2"
                    Name="bField" PropertyName="SelectedValue"
                    ConvertEmptyStringToNull="True" />
                <asp:ControlParameter ControlID="Calendar1"
                    Name="cField" PropertyName="SelectedDate"
                    ConvertEmptyStringToNull="True" />
            </SelectParameters>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newbiealAuthor Commented:
GreymanMSC,

This is great, however shouldn't the select command be OR's instead of AND's?  If I put AND's and the user selects from afield then no records show, but when they select a value from bfield then the filtering pworks.  I want to be able to show all records for afield, when nothing has been selected in bfield and the same the other way around.

Should I leave this code on pageload?

protcted Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If DropDownList1.SelectedIndex.Equals(0) And DropDownList2.SelectedIndex.Equals(0) Then
            data.SelectCommand = ("SELECT * FROM [atable]")
            GridView1.DataBind()
 
        End If
    End Sub
0
GreymanMSCCommented:
How are the DropDownLists being filled?  

They should include a ListItem with a Value of nullstring (""), and perhaps a Text of "-(All)-" or such.  It should be the first item in the list, so it defaults.

This value is converted to Null when passed to the query by the ControlParameters, and thus the (@aField Is Null) condition(s) will evaluate as True.  Thus if no values are selected for all DropDownLists (and Calendar) then all records will show.

Do this and there will be no need to change the SelectCommand to account for non entries, just DataBind.

The following is an example of how to fill a DropDownList using an SqlDataSource with a union query returning a default row.  You can use similar queries if you fill the lists using DataTables, or you can use the AddRow method to programatically create the default row.

<asp:SqlDataSource runat="server" ID="Select4DropDownList1"
     ConnectionString='<%$ ConectionStrings:YourConnectionString %>'
     SelectCommand="SELECT KeyID, KeyText, 0 As First FROM LookupTable
         UNION SELECT Null As KeyID, '-(All)-' As KeyText, -1 As First
         ORDER BY First, KeyText;">
</asp:SqlDataSource>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.