Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a dropdownlist that i want to display years in , but i would like to have a method that automatecly updates the dropdownlist, so i want to have from year 1950-2013 but i want to use datetime, so next year when its 2014 i want the dropdown to automatecly updates so the dropdownn values has from 1950-2014?

Any suggestions ?
Posted

1 solution

Use below code

C#
int startYear = 1950;
var data = Enumerable.Range(startYear, DateTime.Now.Year - (startYear - 1));
                ddl.DataSource = data;
                ddl.DataBind();


Here, ddl is object of DropDownList.
 
Share this answer
 
Comments
Kurac1 29-Apr-13 5:36am    
i would also like to include - Select - in the top of the dropdownlist?
vijay__p 29-Apr-13 5:56am    
After ddl.DataBind(); Add this line....

ddl.Items.Insert(0, new ListItem("- Select -"));

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