title: wpf making calendar using grid
body: <p>I have a specific problem with my calendar I'm making. Its difficult to explain but By determining the week#(of the month) and the dayofweek i make a grid. The dates are not lining up the right way. Example: October 1, 2011 should begin on a saturday, since the 1st is in week 1 but starts on a saturday, the calendar should look something like this: (assuming there's grid squares for each day)  Iknow why this problem is occuring I just don't know how to fix it. The dates are not lining up where they should on the grid.</p>
  
  <p>Calendar should look like this:</p>
  
  <pre>
     Columns       October 2011    grid.column = 7 (0-6)  grid.row = 6 (0-5) (Sunday-Saturday)
  
       Sunday Monday Tuesday Wednesday Thursday Friday Saturday
  row    0      1       2        3       4        5       6
  0     ------------------------------------------------- 1st
  1     2nd     3rd     4th     5th      6th-----------etc..--- 
  2    --------------------------------------------etc...------
  3    ------etc..---------------------------------------------
  4    ---------------- 25th     26th    27th     28th    29th
  5    30th     31st
  
  </pre>
  
  <p>But mine looks like this: (column 0-4 should be shifted down one row)</p>
  
  <pre>
      Sunday Monday Tuesday Wednesday Thursday Friday Saturday
  row    0      1       2        3       4        5       6
  0     2nd     3rd     4th     5th      6th------------- 1st
  1     9th     10th     11th   12th     13th-----------etc..--- 
  2    --------------------------------------------etc...------
  3    ------etc..------25th     26th    27th-----------------
  4    30th     31st--- ---------------------     28th    29th
  5    -------------
  
  </pre>
  
  <p>Code:</p>
  
  <pre><code>public SchedulePage(MainWindow parentForm)
  {
      InitializeComponent();
  
      _parentForm = parentForm;
  
      // DateTime date = new DateTime(year, month, day);
      var t = new List&lt;Schedule&gt;();
      DateTime curr = DateTime.Now;
      DateTime newcurr = new DateTime(curr.Year, curr.Month, 1);
      var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
      var ms = cal.GetWeekOfYear(new DateTime(newcurr.Year, newcurr.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);
  
      for (var i = 1; newcurr.Month == curr.Month; newcurr = newcurr.AddDays(1))
      {
          var sched = new Schedule();
          var month_week = (newcurr.Day / 7) + 1;
          sched.MonthWeek = month_week.ToString();
          sched.Month = newcurr.Month.ToString();
          sched.Year = newcurr.Year.ToString();
          sched.day = newcurr.Day.ToString();
          sched.WeekOfYear = cal.GetWeekOfYear(newcurr, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
          sched.dayofweek = newcurr.DayOfWeek.ToString();
          t.Add(sched);
  
       /* if (newcurr.Day == 1 &amp;&amp; (int)newcurr.DayOfWeek == 7)
          {
              int findspot = month_week - 1;
  
              //_parentForm.bindings.schedule.Add(new Schedule {
              //    WeekNo = month_week - findspot,
              //    WeekDay = (int)newcurr.DayOfWeek,
              //    day = newcurr.Day.ToString()
              //});
          } */
  
          _parentForm.bindings.schedule.Add(new Schedule {
              WeekNo = month_week - 1 ,
              WeekDay = (int)newcurr.DayOfWeek,
              day = newcurr.Day.ToString()
          });
      }
  }
  </code></pre>