I am trying to fill all dates of a selected month in a DataTable. I have done like below mentioned method but its not working.
public DataTable GetDates()
{
DataTable dt = new DataTable();
int year = Convert.ToInt32(ddyear.SelectedItem.Value);//I selected year 2015
int month = Convert.ToInt32(ddmonth.SelectedItem.Value);//I selected month 3
for ( date = new DateTime(year, month, 1); date.Month == month; date = date.AddDays(1))
{
dt.Rows.Add(date);
date = date.AddDays(1);
}
return dt;
}
How can I do it??
public DataTable GetDates()
{
DataTable dt = new DataTable();
dt.Columns.Add("Date", typeof(DateTime));
int year = Convert.ToInt32(ddyear.SelectedItem.Value);
int month = Convert.ToInt32(ddmonth.SelectedItem.Value);
int daysInMonth = DateTime.DaysInMonth(year, month);
for (int i = 0; i < daysInMonth; i++)
{
DataRow dr = dt.NewRow();
dr["Date"] = new DateTime(year, month, i + 1);
dt.Rows.Add(dr);
}
return dt;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.