Click here to Skip to main content
16,021,911 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to design a grid which contain data of weekly allocated project to each resource in ASP.Net MVC + C#

Please help me to design a grid logic where first column is Employee Name and against First Column, 52 more columns started from Calendar Week 39 of the current year to Calendar week 38 of the next year where Each column Header name is the Date-Month of the Monday of that week.

For Example:

For year 2017-18

Column Headers:

[Employee Name|25-Sep|2-Oct|9-Oct|16-Oct|23-Oct|30-Oct|..........|10-Sep|17-Sep]
--------------------------------------------------------------------------------
[Vaibhav|ABC|ABC|ABC|ABC|ABC|ABC|...........|XYZ|XYZ]
--------------------------------------------------------------------------------
[Pandya|XYZ|XYZ|XYZ|XYZ|XYZ|XYZ|...........|ABC|ABC]

Thank You,

What I have tried:

I am new with this grid view concept and logic.
Posted
Updated 23-Oct-17 2:08am

1 solution

It's quite simple. Think of it!
Imagine, you have grid which is binded with DataTable. All what you need to do is to define starting date and ending date. Then you have to loop through 7 days. How? Please see:

C#
CultureInfo myCi = CultureInfo.CurrentCulture;
Calendar myCal = myCi.Calendar;
CalendarWeekRule myCWR = myCi.DateTimeFormat.CalendarWeekRule;
//first Monday of year
DateTime firstMonday = new DateTime(DateTime.Today.Year, 1, 1, myCal);
firstMonday = firstMonday.AddDays(-(int)firstMonday.DayOfWeek + (int)DayOfWeek.Monday);
//start date
DateTime startDate = myCal.AddWeeks(firstMonday, 38);
//end date
DateTime endDate = myCal.AddYears(startDate, 1);
endDate = myCal.AddWeeks(endDate, -1);

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("EmpName", typeof(string)));
    //mondays
firstMonday = startDate;
while(firstMonday<endDate)
{
    dt.Columns.Add(new DataColumn(firstMonday.ToString("dd MMM yyyy", myCi), typeof(string)));
    firstMonday = firstMonday.AddDays(7);
}
//add employee name
dt.Rows.Add("Maciej Los");


For further details, please see:
How to: Extract the Day of the Week from a Specific Date | Microsoft Docs[^]
Calendar.AddWeeks Method (DateTime, Int32) (System.Globalization)[^]
 
Share this answer
 
v2
Comments
Member 13858616 6-Jun-18 0:15am    
Very much helpfull... Thank you sooo much
Maciej Los 6-Jun-18 2:49am    
Great!
Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900