How do i display data that are in between 2 values in a DDL?

102 views Asked by At

I'd like to learn how to code and store the values that are in between (including)

Session("StartDate") = DropDownList2.SelectedValue
Session("EndDate") = DropDownList3.SelectedValue

And if storing the values of DDL2 and DDL3 into a session is wrong then please correct me! The property of the column chosen as the datasource in the access file is of DateTime format by the way.

Greatly Appreciated!

1

There are 1 answers

1
Darren S On

Assuming you are ordering the items so that they are Ascending you could do something like this;

I'm not sure what you are using (WinForms, WebForms etc) so I created a demo in in ASP.net

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList2" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true" runat="server"></asp:DropDownList>
        <asp:DropDownList ID="DropDownList3" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="true" runat="server"></asp:DropDownList>
    </div>
    </form>
</body>
</html>

Code Behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not(Page.IsPostBack) then
            BindDDL(DropDownList2)
            BindDDL(DropDownList3)
        End If
    End Sub

    Public Function GetDates() As IEnumerable(Of DateTime)
        Dim ret = New List(Of DateTime)()

        ret.Add(New DateTime(2015, 1, 1))
        ret.Add(New DateTime(2015, 1, 9))
        ret.Add(New DateTime(2015, 1, 2))
        ret.Add(New DateTime(2015, 1, 3))
        ret.Add(New DateTime(2015, 1, 4))
        ret.Add(New DateTime(2015, 1, 5))
        ret.Add(New DateTime(2015, 1, 6))
        ret.Add(New DateTime(2015, 1, 7))
        ret.Add(New DateTime(2015, 1, 8))

        Return ret

    End Function

    Protected Sub BindDDL(ddl As DropDownList)
        ddl.DataSource = GetDates().OrderBy(Function(x) x)
        ddl.DataBind()
    End Sub

    Protected Sub DropDownList2_SelectedIndexChanged(sender As Object, e As EventArgs)
        GetDateRange(DateTime.Parse(DropDownList2.SelectedValue), DateTime.Parse(DropDownList3.SelectedValue))
    End Sub

    Protected Sub DropDownList3_SelectedIndexChanged(sender As Object, e As EventArgs)
        GetDateRange(DateTime.Parse(DropDownList2.SelectedValue), DateTime.Parse(DropDownList3.SelectedValue))
    End Sub

    Public Function GetDateRange(fromDate As DateTime, toDate As DateTime) As List(Of DateTime)
        Dim ret As List(Of DateTime) = GetDates().where(Function(x) x >= fromDate andalso x <= toDate).OrderBy(Function(x) x).ToList()
        
        'do something with your List(of DateTime)
        'such as write it to the screen or store it in a Session variable 
        
        Return ret
    End Function