Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to display year in dropdownlist dynamically..


it should start with 1950 to till date..

like this..
1950
1951
1952
...
...
...
...
2012
2013
2014


How to do that.......
Posted

you may try with :
C#
int startYear = 1950;
DropDownList1.DataSource = Enumerable.Range(startYear, DateTime.Now.Year - startYear + 1);
DropDownList1.DataBind();
 
Share this answer
 
try this.. :)

C#
for (int Year = 1950; Year <= DateTime.Now.Year; Year++)
           {
               DropDownList1.Items.Add(Year.ToString());
           }
 
Share this answer
 
C#
private void AddYears(int startYear=1955)
   {

       int currentYear = System.DateTime.Now.Year;

       for (int i = startYear; i <= currentYear; i++)
       {
           DropDownList1.Items.Add(new ListItem(Convert.ToString(i)));
       }
   }
 
Share this answer
 
Hi dear priya dharshan
You can Use ComboBox.
this tool exist in Toolbox
after drag and drop this tool into the form, use "Items" property to add your numbers. this property exist in "Properties" window.
thankyou
 
Share this answer
 
Comments
Prasad Avunoori 18-Jun-14 1:06am    
Hi Alireza,

But, she wants to add dynamically. Not hard coding.

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