• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

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

0
newbieal
Asked:
newbieal
  • 2
1 Solution
 
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
 
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

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