Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display year in a dropdown control in desc order 2

Status
Not open for further replies.

taree

Technical User
May 31, 2008
316
US
can someone help me here why the dropdown control is not populated with the year starting from 2009....2000 in a descending order. I can get it to work in ascending order.my problem is when I do it in descending order for asc. i do it this way . thank you

For intYear = DateTime.Now.Year To DateTime.Now.Year + 9

Code:
    Dim intYear As Integer
                   ddlLettYear.Items.Add(intYear)
        Next
        ddlLettYear.Items.Insert(0, New System.Web.UI.WebControls.ListItem("--Year--", 0))
        ddlLettYear.Items.FindByValue(0).Selected = True
[code]
 
Change
Code:
 For intYear = DateTime.Now.Year To DateTime.Now.Year + 9
TO

Code:
 For intYear = DateTime.Now.Year + 9 To DateTime.Now.Year Step -1

   ... Do Stuff

Next

 
this should do it.
Code:
public IEnumerable<int> years_since(int year)
{
   var this_year = DateTime.Today.Year;
   while(this_year >= year)
   {
       yield return this_year--;
   }
}
Code:
ddlLettYear.DataSource = years_since(2000);
ddlLettYear.DataBind();
ddlLettYear.Items.Insert(0, new ListItem("--Year--", 0));

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you both for your valuable tip. It works like a charm I followed jbenson001 way.

For intYear As Integer = DateTime.Now.Year To 1990 Step -1
drpIssueYear.Items.Add(intYear)
Next

drpIssueYear.Items.Insert(0, New System.Web.UI.WebControls.ListItem("--Year--", 0))
drpIssueYear.Items.FindByValue(0).Selected = True
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top