结合与上年下拉 [英] Binding a dropdown with year
本文介绍了结合与上年下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要绑定一个下拉框年从2008年在C#中开始当前年份。我怎样才能实现这一目标。
I have to bind a drop down box with years starting from 2008 to current year in C#. How can I achieve it.
推荐答案
您可以建立一个整数序列与 System.Linq.Enumerable.Range
:
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();
更新:在.NET 2.0中,您可以用迭代器实现自己的范围操作:
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;
}
这篇关于结合与上年下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文