如何使用DateTime.DaysInMonth在几个月内填充Combobox

时间:2015-05-04 16:15:38

标签: c# winforms combobox

我正在尝试使用此

为所选月份中的日期填充ComboBox
    private void cboSelectMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
       if (cboSelectMonth.SelectedIndex >= 0)
       {
            int year = Convert.ToInt32(cboSelectYear.SelectedValue);
            int month = Convert.ToInt32(cboSelectMonth.SelectedValue);

            this.cboSelectDay.DisplayMember = "Text";
            this.cboSelectDay.ValueMember = "Value";

            int dayCount = DateTime.DaysInMonth(year, month);
            var days = new[ dayCount+1 ] { };

            for (int i = 1; i < dayCount +1; i++)
            {
                days[i] = new { Text = Convert.ToString(i), Value = i };
                //cboSelectDay.Items.Add(i);
                //   days[]  { new { Text = Convert.ToString(i), Value = i } };
            }

            this.cboSelectDay.DataSource = days;

            DateTime now = DateTime.Now;
            int dayValue = now.Day;

            cboSelectDay.SelectedIndex = dayValue - 1; 
       }
    }

所以我试图找到一个列出当月所有日子的ComboBox。例如,选择九月将为ComboBox添加30天,选择十月将给你31等。我收到两个错误。第一个是var days = new[ dayCount+1 ] { };行,表示已检测到']'。第二个错误位于days[i] = new { Text = Convert.ToString(i), Value = i };行,其中显示无法隐式转换类型'AnonymousType#1' to 'int'

我正在尝试做类似于我使用Months做的事情,这确实有效(下面的代码块)。我做错了什么?

    private void FillMonthCombobox()
    {
        this.cboSelectMonth.DisplayMember = "Text";
        this.cboSelectMonth.ValueMember = "Value";

        var months = new[] 
        { 
            new { Text = "January", Value = 1 }, 
            new { Text = "February", Value = 2 }, 
            new { Text = "March", Value = 3 }, 
            new { Text = "April", Value = 4 }, 
            new { Text = "May", Value = 5 }, 
            new { Text = "June", Value = 6 }, 
            new { Text = "July", Value = 7 }, 
            new { Text = "Aughust", Value = 8 }, 
            new { Text = "September", Value = 9 }, 
            new { Text = "October", Value = 10 }, 
            new { Text = "November", Value = 11 }, 
            new { Text = "December", Value = 12 } 
        };
        this.cboSelectMonth.DataSource = months;

        DateTime now = DateTime.Now;
        int monthValue = now.Month;
        cboSelectMonth.SelectedIndex = monthValue - 1;
    }

编辑:我现在可以填充ComboBox,但是如何将Text = day和Value = day添加到循环中,以便稍后可以引用该值?在这个循环的情况下,它们将是相同的,但在我正在使用的其他循环的情况下,它们将是不同的。基本上我想在第二个代码块中做同样的事情但是有一个循环。

3 个答案:

答案 0 :(得分:2)

这很简单,但你必须指定一年。心灵二月和闰年!

int year = 2015;
int month = 5;
int[] days = Enumerable.Range(1, DateTime.DaysInMonth(year, month)).ToArray();

您可以在之后将其指定为DataSource

cboSelectDay.DataSource = days;
cboSelectDay.DataBind();

答案 1 :(得分:1)

使用您的一般方法,这确实有效:

int thisMonthsDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);

for (int i = 1; i <= thisMonthsDays; i++) { comboBox1.Items.Add(i);  }

它按照预期的31天用(5月份)填充comboBox1。

尝试更好地掌握并更好地掌握它,我认为此更新将有所帮助:

首先是一个小班:

public class YearClass
    {
        public int IndexOfMonth { get; set; }
        public string DayName { get; set; }
    }

现在有额外的代码将月份的日子绑定到一个comboBox:

 List<YearClass> months = new List<YearClass>();
            int thisMonthsDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);

            for (int i = 1; i <= thisMonthsDays; i++)
            {
                YearClass currentDay = new YearClass();
                    currentDay.IndexOfMonth = i;
                    DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, i);
                    currentDay.DayName = dt.DayOfWeek.ToString();
                    months.Add(currentDay);
            }

            comboBox1.DataSource = months;
            comboBox1.DisplayMember = "DayName";

然后输出如下:

enter image description here

答案 2 :(得分:0)

你可以这样做 - 我正在传递年份和月份的硬编码值

for (int i = 0; i < DateTime.DaysInMonth(2015, 05); i++)
{ 
cmbMonth.Items.Add(i.ToString());
}

如果您有任何其他要求,请告诉我