Reputation: 9
I have 3 comboboxes. 1st: days
,2nd: months
,3rd: years
.
I would like to fill the combobox with the correct days, i think leap year...
for (int i = 1; i <= 12; i++)
{
comboBoxDays.Items.Add(i);
}
for (int i = DateTime.Now.Year; i <= 2050; i++)
{
comboBox1Month.Items.Add(i);
}
How I fill the days with correct numbers? If the year is a leap year.
Upvotes: 1
Views: 1226
Reputation: 375
Try this
int year = 2019;
int month = 4;
int[] days = Enumerable.Range(1, DateTime.DaysInMonth(year, month)).ToArray();
int[] years = Enumerable.Range(DateTime.Now.Year, 2050).ToArray();
string[] months = new string[]{"Jan","Feb","Mar","..."};
comboBoxDays.DataSource = days;
comboBoxDays.DataBind();
comboBoxYears.DataSource = years;
comboBoxYears.DataBind();
comboBox1Month.DataSource = months;
comboBox1Month.DataBind();
Upvotes: 0
Reputation: 169270
There is a DateTime.DaysInMonth method that gives you the number of days in a particular month, even for leap years.
Upvotes: 1