Thursday, January 27, 2011

Genrate calendar in c#


  public DataTable calander(int year, int month)
    {
        DataTable dt = new DataTable();
        DataRow row;
        int k = 1; bool leav = true;
        int day = System.DateTime.DaysInMonth(year, month);
        int da = (int)Convert.ToDateTime(year+"-" + month + "-1"  ).DayOfWeek;
        DataColumn name1 = new DataColumn("SUN", typeof(string));
        DataColumn name2 = new DataColumn("MON", typeof(string));
        DataColumn name3 = new DataColumn("TUE", typeof(string));
        DataColumn name4 = new DataColumn("WED", typeof(string));
        DataColumn name5 = new DataColumn("THU", typeof(string));
        DataColumn name6 = new DataColumn("FRI", typeof(string));
        DataColumn name7 = new DataColumn("SAT", typeof(string));
        dt.Columns.Add(name1); dt.Columns.Add(name2); dt.Columns.Add(name3);  
        dt.Columns.Add(name5); dt.Columns.Add(name6); dt.Columns.Add(name7);
dt.Columns.Add(name4);
        for (int i = 0; i <= 5; i++)
        {
            row = dt.NewRow();
            dt.Rows.Add(row);
            for (int j = 0; j < 7; j++)
            {
                if (da > j && k < 8 && leav)
                { row[j] = ""; }
                else
                {
                    row[j] = k++;
                    leav = false;
                }
                if (k == day + 1) { i++; break; }
            }
        }
        return dt;
    }

No comments:

Post a Comment