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;
}
{
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();
}
{
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
Manoj BhoirPosted May 9, 2015, 11:12 AM
Your question is not clear. Can you provide more details about what exactly you are doing.
Also
You can replace your below 36 lines of code with this one line.
int days = DateTime.DaysInMonth(2015, 05);
Not Required :
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;
}
Sometimes It's Good to be Lazy while coding.
If you want to add dates as columns see this code :
int days = DateTime.DaysInMonth(2015, 05);
DataTable myDT = new DataTable();
for (int i = 1; i <= days; i++)
{
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);
}
If you want to add dates as rows in single column see this code :
int year = 2015;
int month = 5;
int days = DateTime.DaysInMonth(2015, 05);
DataTable myDT = new DataTable();
DataColumn Date_Added = new DataColumn("Date");
Date_Added.ColumnName = "Date";
Date_Added.DataType = System.Type.GetType("System.DateTime");
myDT.Columns.Add(Date_Added);
for (int i = 0; i < days; i++)
{
DataRow dr = myDT.NewRow();
dr[0] = year.ToString() + "-" + month.ToString() + "-" +(i + 1).ToString() ;
myDT.Rows.Add(dr);
}
I hope this will help you.