Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a dropdown with year

Tags:

c#

asp.net

I have to bind a drop down box with years starting from 2008 to current year in C#. How can I achieve it.

like image 588
susanthosh Avatar asked Jun 22 '26 02:06

susanthosh


1 Answers

You can build a sequence of integers with System.Linq.Enumerable.Range:

var startYear = 2008;
myDropDownList.DataSource = Enumerable.Range(startYear, DateTime.Now.Year - startYear + 1);
myDropDownList.DataBind();

Enumerable.Range on MSDN

Update: In .NET 2.0 you can implement your own Range operator with an iterator:

public static IEnumerable<int> Range (int start, int count)
{
    int end = start + count;

    for (int i = start; i < end; i++) 
        yield return i;
}
like image 162
dahlbyk Avatar answered Jun 23 '26 14:06

dahlbyk