I would like to populate an with a range of years using VB.Net. I know how to populate a DropDownList with just a single year, but I'm interested in a two year span (i.e. 2003-2004). I know I can put the range values into a database, but I was wondering if
there was a way to use VB's built in Date features to accomplish this. Thank You.
Dim intYear As Integer = DateTime.Now.Year
Dim i As Integer = 0
For i = 0 To 4 ' 5 years
ddlList.Items.Add(New ListItem((intYear + i).ToString() + " - " + (intYear + i + 1).ToString(), (intYear + i).ToString()))
Next
Thanks pickyh3d that was exactly what I was looking for. I would like to add 10 years before the current span (2004-2005) and 10 years after the current span, but I can't get it to appear in the DropDownList correctly. Here is what I have. For x = 0 To 10 ddlSchoolYearEnteredProgram.Items.Add(New
ListItem((myYear - x).ToString() & " - " & (myYear - x + 1).ToString(), (intYear - i).ToString())) Next For x = 1 To 10 ddlSchoolYearEnteredProgram.Items.Add(New ListItem((myYear + x).ToString() & " - " & (myYear + x + 1).ToString(), (intYear + i).ToString()))
Next This displays: 2004 - 2005 2003 - 2004 ....... - ....... 1994 - 1995 2005 - 2006 2006 - 2007 ....... - ........ If you could help me out again I would appriciate it. Thanks again.
Hey, Change to subtraction if you want to go backwards in years. Brian
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
Use the same loop as I provided, but subtract 10 years right from the start.
Dim intYears As Integer = DateTime.Now.Year
Years(intYears - 10)
Years(intYears)
...
Sub Years(ByVal intYear As Integer)
For x = 1 To 10
ddlSchoolYearEnteredProgram.Items.Add(New ListItem((intYear + x).ToString() & " - " & (intYear + x + 1).ToString(), (intYear + x).ToString()))
Next
End Sub
Thanks again pickyh3d. I just have one more question on this subject (I promise). Since you are subtracting ten years from the DateTime.Now.Year, is there any way to display the current range (2004-2005) in the DropDownList initially? I have the range of dates
that I need and in the proper order, but it would be nice if the current range was displayed when the page loaded. Thanks again for all of your help.
awoomer
Member
636 Points
174 Posts
Populating a DropDownList with a Range of Years (i.e. 2003 -2004)
Nov 08, 2004 05:46 PM|LINK
pickyh3d
Star
9696 Points
1955 Posts
Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004)
Nov 08, 2004 08:42 PM|LINK
awoomer
Member
636 Points
174 Posts
Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004)
Nov 09, 2004 03:01 PM|LINK
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004)
Nov 09, 2004 04:10 PM|LINK
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
awoomer
Member
636 Points
174 Posts
Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004)
Nov 09, 2004 04:53 PM|LINK
pickyh3d
Star
9696 Points
1955 Posts
Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004)
Nov 09, 2004 10:10 PM|LINK
Dim intYears As Integer = DateTime.Now.Year Years(intYears - 10) Years(intYears) ... Sub Years(ByVal intYear As Integer) For x = 1 To 10 ddlSchoolYearEnteredProgram.Items.Add(New ListItem((intYear + x).ToString() & " - " & (intYear + x + 1).ToString(), (intYear + x).ToString())) Next End Subawoomer
Member
636 Points
174 Posts
Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004)
Nov 10, 2004 01:32 PM|LINK
pickyh3d
Star
9696 Points
1955 Posts
Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004)
Nov 12, 2004 01:47 AM|LINK
awoomer
Member
636 Points
174 Posts
Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004)
Nov 12, 2004 01:00 PM|LINK