Reputation: 1574
I am trying to fill a ComboBox with the days in the selected month with 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 end up with a ComboBox that lists all days from the current month. For instance, choosing September is going to add 30 days tot the ComboBox and choosing October would give you 31 etc. I am getting two errors. The first is on the var days = new[ dayCount+1 ] { };
line, which says that a ']'
is exptected. The second error is on the days[i] = new { Text = Convert.ToString(i), Value = i };
line which says Cannot implicitly convert type 'AnonymousType#1' to 'int'
I am trying to do something similar to what I am doing with the Months, which does work (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 populate the ComboBox now but how do I add the Text = day and the Value = day to the loop so I can reference the Value later? In the case of this loop they will be the same but in the case of some other loops I am working with they will be different. Essentially I want to do the same thing I am doing in the second code block but with a loop.
Upvotes: 3
Views: 5164
Reputation: 5140
Using your general approach, this does work:
int thisMonthsDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
for (int i = 1; i <= thisMonthsDays; i++) { comboBox1.Items.Add(i); }
It fills a comboBox1 with (for May) with 31 days as expected.
Trying to work through and grasp 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 additional code to bind the month's days to a 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 output then looks like:
Upvotes: 1
Reputation: 470
you can do this - I am passing hard coded 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 any other requirement
Upvotes: 0
Reputation: 14880
It's simple, but you have to specify a year, too. Mind February and leap years!
int year = 2015;
int month = 5;
int[] days = Enumerable.Range(1, DateTime.DaysInMonth(year, month)).ToArray();
You can specify it as a DataSource
afterwards:
cboSelectDay.DataSource = days;
cboSelectDay.DataBind();
Upvotes: 2