Binding a dropdown with year

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


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

<strong>Update:</strong> 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; }

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:Binding a dropdown with year