how to add date column to get dates

May 9 2015 9:06 AM
i have created form named student attendance. i want to display dates in grid view on change of month.
 
this is my code
int GetDaysInMonth(int month, int year)
{
if (month < 1 || month > 12)
{
throw new System.ArgumentOutOfRangeException("month", month, "month mustbe between 1 and 12");
}
if (1 == month || 3 == month || 5 == month || 7 == month || 8 == month ||
10 == month || 12 == month)
{
return 31;
}
else if (2 == month)
{
// Check for leap year
if (0 == (year % 4))
{
// If date is divisible by 400, it's a leap year.
// Otherwise, if it's divisible by 100 it's not.
if (0 == (year % 400))
{
return 29;
}
else if (0 == (year % 100))
{
return 28;
}

// Divisible by 4 but not by 100 or 400
// so it leaps
return 29;
}
// Not a leap year
return 28;
}
return 30;
}
 
protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
int days = GetDaysInMonth(Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(DateTime.Now.Year));
DataTable myDT = new DataTable();
for (int i = 0; i < days; i++)
{
//DataColumn id = new DataColumn("id");
//id.ColumnName = id + i.ToString();
//id.DataType = System.Type.GetType("System.Int32");
//myDT.Columns.Add(id);
DataColumn Date_Added = new DataColumn("Date");
Date_Added.ColumnName = Date_Added + i.ToString();
Date_Added.DataType = System.Type.GetType("System.DateTime");
myDT.Columns.Add(Date_Added);
}

for (int i = 1; i <= 1; i++)
{
DataRow dr = myDT.NewRow();
for (int ii = 0; ii < days; ii++)
{
dr[ii] = ii.ToString();
}
myDT.Rows.Add(dr);
}
gvStudAttendance.DataSource = myDT;
gvStudAttendance.DataBind();
}
please tell me how to add date column to get dates of dat purticular month

Answers (1)