简体   繁体   English

用日期(仅年和月)填充组合框?

[英]Fill combobox with dates (only years and months)?

DateTime now = DateTime.Now;
List<DateTime> dates = new List<DateTime>();
dates.Add(now);
dates.Add(now.AddMonths(6));
dates.Add(now.AddMonths(1));
dates.Add(now.AddYears(1));

comboBoxDates.DataSource = dates;

I would use a loop and go through the list, and make another list without the days and house:minutes.. but I'm sure there are better ways to do this. 我将使用循环并遍历该列表,然后再创建一个没有天数和房屋:分钟数的列表。.但是我敢肯定,有更好的方法可以做到这一点。 Thank you in advance! 先感谢您!

It depends on what you are trying to achieve, but in general, you can bind List<DateTime> to your combobox like you do it and then just set its FormatString to whatever you need: 这取决于您要实现的目标,但是通常,您可以像这样将List<DateTime>绑定到组合框,然后将其FormatString设置为所需的值:

comboBoxDates.FormatString = "MM-yyyy";

This approach allows you to configure what is being displayed to users, and in code you can still work with DateTime bound. 这种方法允许您配置要显示给用户的内容,并且在代码中您仍然可以使用DateTime绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM