Click here to Skip to main content
15,888,148 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I was wondering how to add years to dropdown list,

for example I want to start with 2013

then today is 2014 so it is added to the dropdown,

then next year is 2015 so 2015 will be added


how do I do this?
Posted

Hello friend, Try this code:

C#
public void PopulateYears(int startYear)
{
    List<int> years = new List<int>();
    int currentYear = DateTime.Now.Year;
    for (int year = startYear; year <= currentYear; year++)
    {
        years.Add(year);
    }

    // Bind the dropdownlist
    ddlYears.DataSource = years;
    ddlYears.DataBind();
}



- DD
 
Share this answer
 
XML
int currentYear =Convert.ToInt32(DateTime.Now.Year);
      List<int> yrs = new List<int>();

      for (int i = 2013; i <= currentYear; i++)
      {
          yrs.Add(i);
      }


then u can bind a list to dropdown hope this will help
 
Share this answer
 

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