question

PadmanabhanVenkatesh-6789 avatar image
0 Votes"
PadmanabhanVenkatesh-6789 asked cooldadtx commented

Display last 4 weekend dates dynamically

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# ?

dotnet-csharpdotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
1 Vote"
cooldadtx answered cooldadtx commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi .
Thanks for the reply.
The page should refresh for every Monday with last 4 weekends as header . How to achieve this ?

0 Votes 0 ·
cooldadtx avatar image cooldadtx PadmanabhanVenkatesh-6789 ·

What do you mean by "refresh for monday"? Do you mean your start date is always a Monday? Doesn't really matter for the code I gave. If you need the Monday of the week you're on it is identical to finding the Saturday before, just the offsets are different. It almost sounds like you are building a calendar except the weekends are combined into 1 column.

0 Votes 0 ·