我必须在 C# 中绑定(bind)一个从 2008 年到当年的下拉框。我怎样才能实现它。
最佳答案
您可以使用 System.Linq.Enumerable.Range
构建一个整数序列:
var startYear = 2008;
myDropDownList.DataSource = Enumerable.Range(startYear, DateTime.Now.Year - startYear + 1);
myDropDownList.DataBind();
更新:在 .NET 2.0 中,您可以使用迭代器实现您自己的范围运算符:
public static IEnumerable<int> Range (int start, int count)
{
int end = start + count;
for (int i = start; i < end; i++)
yield return i;
}
关于c# - 将下拉菜单与年份绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1508594/