Generate Attendance Details for an year using WPF Datagrid- What is the best way to do it?

Viewed 1985

I am using below code to get the year wise attendance dashboard details for an employee using wpf DataGrid.The actual problem is am pretty confused to bind the data with wpf datagrid columns.I want to generate all grid columns dynamically and bind the data into it.My requirement is display the days in column headers and every row data starting position should be based on the 1st day of month.Find the attached image for more clarity.

Shall i generate the Model for every column like FirstSunday,FirstMonday likewise these till Fifth Saturday of week to bind the column or is there any other way to do it easily. Any help would be highly appreciated.

enter image description here

Below code used to generate all days and month names with holiday details

   public class DashboardDateDetails
    {
        public bool IsHoliday { get; set; }
        public string DayName { get; set; }
        public string ShortDate { get; set; }
        public DateTime Date { get; set; }
        public string MonthWithYear { get; set; }
        public string ReasonForHoliday { get; set; }
    }

//Input parameter HolidaysList will hold the list of holidays in a year.This method would return the date and day with holiday details from 1st Jan to 31st Dec for a selected year.

private List<DashboardDateDetails> GetDashBoardData(List<KeyValuePair<string,DateTime>> HolidaysList)
{
    List<DashboardDateDetails> MonthList = new List<DashboardDateDetails>();
    int CurrentYear = DateTime.Now.Year;
    DateTime FirstDateOfYear = new DateTime(CurrentYear,01,01);            
    System.Globalization.CultureInfo Culture = new System.Globalization.CultureInfo("en-US");
    string[] DayNames = Culture.DateTimeFormat.AbbreviatedDayNames;
    string[] MonthNames = Culture.DateTimeFormat.MonthNames;
    string FirstDayNameOfYear = DayNames[(int)FirstDateOfYear.DayOfWeek];
    for (int MonthCount = 1; MonthCount <= 12; MonthCount++)
    {
        int NumberOfDaysInMonth = DateTime.DaysInMonth(CurrentYear, MonthCount);
        for (int DayCount = 1; DayCount <= NumberOfDaysInMonth; DayCount++)
        {                    
            DashboardDateDetails DateDetails = new DashboardDateDetails();
            DateTime CurrentDate = new DateTime(CurrentYear, MonthCount, DayCount);
            DateDetails.DayName = DayNames[(int)CurrentDate.DayOfWeek];
            DateDetails.Date = CurrentDate;
            DateDetails.ShortDate = CurrentDate.ToShortDateString();
            DateDetails.MonthWithYear = MonthNames[(int)CurrentDate.Month - 1];
            if (HolidaysList != null && HolidaysList.Any())
            {
                var HolidayDate = HolidaysList.Where(a => a.Value.ToShortDateString() == CurrentDate.ToShortDateString());
                DateDetails.IsHoliday = HolidayDate != null && HolidayDate.Any();
                DateDetails.ReasonForHoliday = HolidayDate != null && HolidayDate.Count() > 0 ? HolidayDate.First().Key : string.Empty;
            }
            MonthList.Add(DateDetails);
        }
    }
    return MonthList;
}
1 Answers
Related