Create Weekly Calendar table between two Dates using C# and VB.Net in ASP.Net

edvinJohn
 
on Feb 17, 2020 07:41 AM
Sample_231928.zip
1493 Views

How to create Weekly table Using Two Input dates 

// Public Holidays Background Colour yellow

// other holidays backgroung colour Red like sunday and saturday

// And Calculate the Number of working days. 

Sunday

Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

1/1/2020

2/1/2020

3/1/2020

4/1/2020

5/1/2020

6/1/2020

7/1/2020

8/1/2020

9

10

11

12

13

14

15

16/1/2020

17

18

19

20

21

22

23

24

25

26/1/2020

27

28

29

30

31

1

 

 

 

 

string fromDate = "1/1/2020";
string todate = "1/2/2020";
FDate = Convert.ToDateTime(fromDate);
TDate = Convert.ToDateTime(todate);
List<DateTime> holidays = new List<DateTime>();
// Manually adding all holiday list.
holidays.Add(new DateTime(DateTime.Now.Year, 1, 1)); // holidays
holidays.Add(new DateTime(DateTime.Now.Year, 1, 16)); // holodays
holidays.Add(new DateTime(DateTime.Now.Year, 1, 26)); // National Holiday.

Need help to solve this

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arunkurmi
 
on Feb 18, 2020 04:52 AM

Hi edvinJohn,

Check this sample. now take its reference.

HTML

<asp:GridView ID="gvWeeklyCalender" runat="server">
</asp:GridView><br />
<asp:Label ID="lblWorkingDays" runat="server" />

Namespaces

C#

using System.Data;
using System.Drawing;

VB

Imports System.Data
Imports System.Drawing

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string fromDate = "1/1/2020";
        string todate = "2/1/2020";
        DateTime startDate = Convert.ToDateTime(fromDate);
        DateTime endDate = Convert.ToDateTime(todate);
        DataTable dt = new DataTable();
        dt.Columns.Add("Sunday");
        dt.Columns.Add("Monday");
        dt.Columns.Add("Tuesday");
        dt.Columns.Add("Wednesday");
        dt.Columns.Add("Thursday");
        dt.Columns.Add("Friday");
        dt.Columns.Add("Saturday");
        DataRow dr = dt.NewRow();

        for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
        {
            int i = (int)date.DayOfWeek;
            string day = date.DayOfWeek.ToString();
            dr[day] = date.ToShortDateString();
            if (i == 6)
            {
                dt.Rows.Add(dr);
                dr = dt.NewRow();
            }
        }

        this.gvWeeklyCalender.DataSource = dt;
        this.gvWeeklyCalender.DataBind();

        List<DateTime> holidays = new List<DateTime>();
        // Manually adding all holiday list.
        holidays.Add(new DateTime(DateTime.Now.Year, 1, 1)); // holidays
        holidays.Add(new DateTime(DateTime.Now.Year, 1, 16)); // holodays
        holidays.Add(new DateTime(DateTime.Now.Year, 1, 26)); // National Holiday.

        int count = 0;
        foreach (GridViewRow row in this.gvWeeklyCalender.Rows)
        {
            foreach (TableCell cell in row.Cells)
            {
                if (cell.Text != "&nbsp;")
                {
                    var date = holidays.Where(x => x.ToShortDateString() == cell.Text).FirstOrDefault();
                    if (date != DateTime.MinValue)
                    {
                        cell.ForeColor = Color.Green;
                    }
                    else if ((int)Convert.ToDateTime(cell.Text).DayOfWeek == 0 || (int)Convert.ToDateTime(cell.Text).DayOfWeek == 6)
                    {
                    }
                    else
                    {
                        count++;
                    }
                }
            }
            row.Cells[0].ForeColor = Color.Red;
            row.Cells[6].ForeColor = Color.Red;
        }

        lblWorkingDays.Text = "Total working Days : " + count.ToString();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim fromDate As String = "1/1/2020"
        Dim todate As String = "2/1/2020"
        Dim startDate As DateTime = Convert.ToDateTime(fromDate)
        Dim endDate As DateTime = Convert.ToDateTime(todate)
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("Sunday")
        dt.Columns.Add("Monday")
        dt.Columns.Add("Tuesday")
        dt.Columns.Add("Wednesday")
        dt.Columns.Add("Thursday")
        dt.Columns.Add("Friday")
        dt.Columns.Add("Saturday")
        Dim dr As DataRow = dt.NewRow()
        Dim sdate As DateTime = startDate
        While sdate <= endDate
            Dim i As Integer = CInt(sdate.DayOfWeek)
            Dim day As String = sdate.DayOfWeek.ToString()
            dr(day) = sdate.ToShortDateString()
            If i = 6 Then
                dt.Rows.Add(dr)
                dr = dt.NewRow()
            End If
            sdate = sdate.AddDays(1)
        End While
        Me.gvWeeklyCalender.DataSource = dt
        Me.gvWeeklyCalender.DataBind()
        Dim holidays As List(Of DateTime) = New List(Of DateTime)()
        holidays.Add(New DateTime(DateTime.Now.Year, 1, 1))
        holidays.Add(New DateTime(DateTime.Now.Year, 1, 16))
        holidays.Add(New DateTime(DateTime.Now.Year, 1, 26))
        Dim count As Integer = 0
        For Each row As GridViewRow In Me.gvWeeklyCalender.Rows
            For Each cell As TableCell In row.Cells
                If cell.Text <> "&nbsp;" Then
                    Dim datee = holidays.Where(Function(x) x.ToShortDateString() = cell.Text).FirstOrDefault()
                    If datee <> DateTime.MinValue Then
                        cell.ForeColor = Color.Green
                    ElseIf CInt(Convert.ToDateTime(cell.Text).DayOfWeek) = 0 OrElse CInt(Convert.ToDateTime(cell.Text).DayOfWeek) = 6 Then
                    Else
                        count += 1
                    End If
                End If
            Next
            row.Cells(0).ForeColor = Color.Red
            row.Cells(6).ForeColor = Color.Red
        Next
        lblWorkingDays.Text = "Total working Days : " & count.ToString()
    End If
End Sub

Screenshot

Screenshot