Click here to Skip to main content
15,923,910 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
How to load dropdownlist, currentyear, previous year and next two years.

Please Help me.

Thanks in Advance.
Posted
Updated 23-May-11 8:10am
v3

Hope this[^] might help you.
 
Share this answer
 
Have you not tried the simple route of current year -1, current year, current year +1 and current year +2?

Seems straightforward to me.
<br />
DateTime.Now.Year.ToString()
for current year.
Just add and subtract as necessary.
 
Share this answer
 
Comments
Espen Harlinn 22-May-11 11:33am    
Good points, my 5
C#
void BindDropdown()
   {
       for (int i = -1; i <= 2; i++)
       {
           string date = DateTime.Now.AddYears(i).ToString();
           DropDownList1.Items.Add(date);

       }
   }


call it on form load event of a page
 
Share this answer
 
Try a function something like this

C#
List<string> years = new List<string>();
           for (int i = -1; i <= 2; ++i)
           {
               years.Add(DateTime.Now.AddYears(i).ToString("yyyy"));
           }


Then you can bind your DropDownList to years

Hope this helps
 
Share this answer
 
Hi,

This is the same as the previous answers, but it uses another way of using a for-loop:

C#
List<string> years = new List<string>();

for (DateTime date = DateTime.Now.AddYears(-1); date <= DateTime.Now.AddYears(2); date = date.AddYears(1))
{
    years.Add(date.Year.ToString("yyyy"));
}


I found out that date-loops (also day- / month- / hour- / minute- loops) are possible this way and I just wanted to share this ;)


Hope this helps someone.


Best regards and happy coding,
Stops
 
Share this answer
 
I Use this code, just from now to 7 or 8 years back.
Me.ComboBox2.Items.Add(Convert.ToString(Convert.ToInt16(Year(DateTime.Now))))
Me.ComboBox2.Items.Add(Convert.ToString(Convert.ToInt16(Year(DateTime.Now)) - 1))
Me.ComboBox2.Items.Add(Convert.ToString(Convert.ToInt16(Year(DateTime.Now)) - 2))
Me.ComboBox2.Items.Add(Convert.ToString(Convert.ToInt16(Year(DateTime.Now)) - 3))
Me.ComboBox2.SelectedIndex = 0
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900