user1708328
user1708328

Reputation: 51

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!

Upvotes: 0

Views: 4948

Answers (1)

Michal Klouda
Michal Klouda

Reputation: 14521

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:

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.

Upvotes: 3

Related Questions