How to fill Combobox with days in month using DateTime.DaysInMonth

I am trying to fill a ComboBox with days in a selected month using this

    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; 
       }
    }

      

So I am trying to get a ComboBox that lists all the days from the current month. For example, choosing September is going to add 30 days to the ComboBox, while choosing October will give you 31, etc. I am getting two errors. The first is on the line var days = new[ dayCount+1 ] { };

that says a ']'

. The second error is on the line days[i] = new { Text = Convert.ToString(i), Value = i };

that says "Can't implicitly convert'AnonymousType#1' to 'int'

I am trying to do something similar to what I am doing with Months that works (code block below). What am I doing wrong?

    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;
    }

      

Edit: I can now fill the ComboBox, but how do I add a Text = day and Value = day loop to the loop so that I can refer to the Value later on? In the case of this loop, they will be the same, but in the case of some of the other loops I work with, they will be different. Basically, I want to do the same thing as in the second block of code, but with a loop.

+3


source to share


3 answers


It's simple, but you must also include the year. Um February and leap years!

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

      



Then you can specify it like DataSource

:

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

      

+2


source


Using your general approach, this works:

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

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

      

It fills comboBox1 with (as of May) with 31 days as expected.

Trying to work and understand it better, I think this update will help:

First a small class:



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

      

And now the extra code to bind the days of the month to the 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";

      

The result looks like this:

enter image description here

+1


source


you can do this - i am passing hardcoded values ​​for year and month

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

      

let me know if you have other requirements

0


source







All Articles