简体   繁体   English

如何用天、月和年填充组合框?

[英]How to fill comboboxes with days, months and years?

I have 3 comboboxes.我有 3 个组合框。 1st: days , 2nd: months , 3rd: years . 1st: days2nd: months3rd: 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.如果年份是闰年。

有一个DateTime.DaysInMonth方法可以为您提供特定月份的天数,即使是闰年。

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();

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

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