Display last 4 weekend dates dynamically

Padmanabhan, Venkatesh 241 Reputation points
2021-08-10T11:49:51.403+00:00

Hi.
I am trying to use a gridview in a ASP.NET web application, to check if a report is uploaded or not.

Example format, which I am trying to achieve :

Team Name 31 July-1 Aug 24 July-25 July 17 July-18 July 10 July-11July

Team A Uploaded NA NotUploaded Uploaded
Team B Uploaded NA NotUploaded Uploaded

How to get the last 4 weeks of weekend as header, when this page is viewed on 2nd Aug. The page should refresh for every Monday with last 4 weeks as header ? How to achieve this in gridview and C# ?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,325 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,412 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 49,251 Reputation points
    2021-08-10T16:00:21.853+00:00

    Assuming you already have the columns in the grid and you're simply customizing the headers then it is a simple matter of calculating the weekend dates. If you don't even have those columns in your dataset yet then you'd need to add the columns dynamically on the server side which is also straightforward. But since there is no data in your binding dataset then there would be nothing to display. I'm going to assume you have the data in your dataset that your binding to and therefore the columns will have data and you just want to customize the header.

    Calculate the previous Saturday by taking the current date (which could be a Saturday) and calculate the offset to previous Saturday (e.g. Saturday would be 0, Sunday would be 1, Monday would be 2, etc)

    //Jump to Saturday before (keep in mind we might already be on it
    var offset = (date.DayOfWeek != DayOfWeek.Saturday) ? (int)date.DayOfWeek + 1 : 0;
    var saturday = date.AddDays(-offset);
    var sunday = saturday.AddDays(1);
    

    Note I'm breaking the expression up to make it easier to read, you can combine as needed. date is the date you want to get the previous Saturday of. Here I'm assuming that if you're on a Saturday then you want that day.

    To get the weekends before that just keep AddDays(-7) to the Saturday date.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful