Populating a DropDownList with a Range of Years (i.e. 2003 -2004)http://forums.asp.net/t/741533.aspx/1?Populating+a+DropDownList+with+a+Range+of+Years+i+e+2003+2004+Fri, 12 Nov 2004 13:40:24 -0500741533741533http://forums.asp.net/p/741533/741533.aspx/1?Populating+a+DropDownList+with+a+Range+of+Years+i+e+2003+2004+Populating a DropDownList with a Range of Years (i.e. 2003 -2004) 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. 2004-11-08T17:46:51-05:00741720http://forums.asp.net/p/741533/741720.aspx/1?Re+Populating+a+DropDownList+with+a+Range+of+Years+i+e+2003+2004+Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004) <pre class="prettyprint">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 &#43; i).ToString() &#43; &quot; - &quot; &#43; (intYear &#43; i &#43; 1).ToString(), (intYear &#43; i).ToString())) Next</pre> 2004-11-08T20:42:06-05:00742346http://forums.asp.net/p/741533/742346.aspx/1?Re+Populating+a+DropDownList+with+a+Range+of+Years+i+e+2003+2004+Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004) 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() &amp; &quot; - &quot; &amp; (myYear - x &#43; 1).ToString(), (intYear - i).ToString())) Next For x = 1 To 10 ddlSchoolYearEnteredProgram.Items.Add(New ListItem((myYear &#43; x).ToString() &amp; &quot; - &quot; &amp; (myYear &#43; x &#43; 1).ToString(), (intYear &#43; 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. 2004-11-09T15:01:08-05:00742432http://forums.asp.net/p/741533/742432.aspx/1?Re+Populating+a+DropDownList+with+a+Range+of+Years+i+e+2003+2004+Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004) Hey, Change to subtraction if you want to go backwards in years. Brian 2004-11-09T16:10:43-05:00742482http://forums.asp.net/p/741533/742482.aspx/1?Re+Populating+a+DropDownList+with+a+Range+of+Years+i+e+2003+2004+Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004) Thanks for the reply Brian, but I want to go forward and backwards at the same time. I just can't get the years to be in order in the DropDownList. 2004-11-09T16:53:01-05:00742793http://forums.asp.net/p/741533/742793.aspx/1?Re+Populating+a+DropDownList+with+a+Range+of+Years+i+e+2003+2004+Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004) Use the same loop as I provided, but subtract 10 years right from the start.<pre class="prettyprint">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 &#43; x).ToString() &amp; &quot; - &quot; &amp; (intYear &#43; x &#43; 1).ToString(), (intYear &#43; x).ToString())) Next End Sub</pre> 2004-11-09T22:10:56-05:00743229http://forums.asp.net/p/741533/743229.aspx/1?Re+Populating+a+DropDownList+with+a+Range+of+Years+i+e+2003+2004+Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004) 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. 2004-11-10T13:32:31-05:00744972http://forums.asp.net/p/741533/744972.aspx/1?Re+Populating+a+DropDownList+with+a+Range+of+Years+i+e+2003+2004+Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004) After filling the drop down list, do this:<pre class="prettyprint">. . . Years(intYears) ddlSchoolYearEnteredProgram.Items.FindByValue(intYears.ToString()).Selected = True</pre>If there is already a selected item, then you will have to unselect (ddl.SelectedItem.Selected = False) BEFORE you select the next item. 2004-11-12T01:47:09-05:00745288http://forums.asp.net/p/741533/745288.aspx/1?Re+Populating+a+DropDownList+with+a+Range+of+Years+i+e+2003+2004+Re: Populating a DropDownList with a Range of Years (i.e. 2003 -2004) Thanks a million pickyh3d!!! Everything works as it should now. Thanks again. 2004-11-12T13:00:19-05:00