Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how to fill year in combobox with using for loop(validate through current year-1950)
Posted

hi u can do as follows:

C#
ddl.Items.Insert(0, "---Select Year---");
int index = 1;
for (int i = 2010; i > 1950; i--)
{
  ddl.Items.Insert(index, new ListItem(i.ToString(), i.ToString()));
  index++;
}

and dropdown is created as

ASP
<asp:dropdownlist id="ddl" runat="server" xmlns:asp="#unknown"></asp:dropdownlist>


[Modified: pre tags are your friend!]
 
Share this answer
 
v2
Hi, use below code

C#
for (int i = DateTime.Now().Year; i > 1950; i--)
{
  ddl.Items.Add(new ListItem(i.ToString(), i.ToString()));
}

ddl.Items.Insert(0,"---Select---");
 
Share this answer
 
Atleast you might post some code you have tried, anyway try this sample code how to add years to dropdownlist[^]. Change code based on your requirement.
 
Share this answer
 
Try this:
VB
For i = DateTime.Now.Year - 15 To DateTime.Now.Year
     DropDownList1.Items.Add(i)
Next

and thats it....

Happy Coding

[Modified: use pre tags for code!]
 
Share this answer
 
v2
C#
cmbyear.Items.Clear();
for (int i = 2007; i <= DateTime.Now.Year; i++)
{
    cmbyear.Items.Add(i);
}
 
Share this answer
 
Dim year As String
year = 2011
With CmbYear
While year <= Date.Now().Year
.Items.Add(year)
year = year + 1
End While
End With
 
Share this answer
 
Comments
CHill60 20-Dec-13 10:29am    
Reason for my downvote - question is 3.5years old and was correctly resolved by Solution 3

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