c# - 如何获得“jan-2018, Feb-2018, Mar-2018”作为下拉列表
问题描述
如何在下拉列表中同时获取月份和年份,月份列表应显示下一个 3 个月列表,例如,如果当前月份是 2018 年 1 月,则下拉列表应显示 2018 年 1 月、2018 年 2 月、2018 年 3 月。
谁能建议我如何实现这一目标?
Jan-2018
Feb-2018
Mar-2018
解决方案
一种简单的方法是使用inEnumerable.Range
并获取正确的月份名称GetMonthName
DateTimeFormatInfo
DropDownList1.DataSource = Enumerable.Range(1, 12).Select(i => new KeyValuePair<int, string>(i, DateTimeFormatInfo.CurrentInfo.GetMonthName(i).Substring(0, 3) + "-" + DateTime.Now.Year)).ToList();
DropDownList1.DataValueField = "key";
DropDownList1.DataValueField = "value";
DropDownList1.DataBind();
DDL 的 value 字段仍然是 1 - 12 的整数,因此您可以轻松地在代码中处理月份。
更新
如果您想从当前月份开始,请使用
Enumerable.Range(0, 11).Select(i => new KeyValuePair<int, string>(i, DateTimeFormatInfo.CurrentInfo.GetMonthName(DateTime.Now.AddMonths(i+1).Month).Substring(0, 3) + "-" + DateTime.Now.AddMonths(i).Year)).ToList();
推荐阅读
- c# - 我有一张很长的照片,我想在 Unity 中下载它我收到此错误:(纹理的宽度/高度超出范围)我该怎么办
- windows - 为什么 Atom 会这样?
- r - 减少ggplot2水平图例中项目之间的间距
- laravel - Laravel 8 未定义的变量
- excel - 基于表 2 列的表 1 列中部分字符串的 PowerPivot 计数出现次数
- android - 在 Android AOSP 源代码中阻止应用访问互联网
- android-studio - Android Studio - 如何在输出控制台中隐藏运行命令文本
- javascript - 带有小数点值的数字问题
- java - 返回静态函数内的数据类型角度
- facebook - Facebook 对话链接