Generate ASP.Net GridView Rows based on Date range defined in TextBox using C# and VB.Net

Last Reply 7 months ago By pandeyism

Posted 7 months ago

I want date range between two ranges and repeat ID like that

Date Range From Textbox  01/12/2018 to 4/12/2018 

ID        Date

01      01/12/2018

01      02/12/2018

01      03/12/2018

01      04/12/2018

02      01/12/2018

02      02/12/2018

02      03/12/2018

02      04/12/2018 

        protected void Button1_Click(object sender, EventArgs e)
        {
            string dateString = TextBox1.Text;
            DateTime date1 = Convert.ToDateTime(dateString, System.Globalization.CultureInfo.GetCultureInfo("ur-PK").DateTimeFormat);
            string dateString2 = TextBox2.Text;
            DateTime date2 = Convert.ToDateTime(dateString, System.Globalization.CultureInfo.GetCultureInfo("ur-PK").DateTimeFormat);
           
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("EmployeeId", typeof(int)), new DataColumn("Date", typeof(string)) });
            for (DateTime i = DateTime.Parse(TextBox1.Text); i <= DateTime.Parse(TextBox2.Text); i = i.AddDays(1))
            {
                //  for (int j = 1; j <= 30; j++)
                {
                    dt.Rows.Add( i.ToString("dd-MMM-yyyy") +"   " + i.DayOfWeek);
                }
            }
            this.gvEmployees.DataSource = dt;
            this.gvEmployees.DataBind();

 

You are viewing reply posted by: pandeyism 7 months ago.
Posted 7 months ago

Hey akhter,

Please refer below sample.

HTML

<div>
    From Date:
    <asp:TextBox ID="TextBox1" runat="server" />
    End Date:
    <asp:TextBox runat="server" ID="TextBox2" />
    <asp:Button ID="Button1" Text="Insert" runat="server" OnClick="Insert" />
    <asp:GridView runat="server" ID="gvEmployees" />
</div>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Insert(object sender, EventArgs e)
{
    string dateString = TextBox1.Text;
    DateTime date1 = Convert.ToDateTime(dateString, System.Globalization.CultureInfo.GetCultureInfo("ur-PK").DateTimeFormat);
    string dateString2 = TextBox2.Text;
    DateTime date2 = Convert.ToDateTime(dateString2, System.Globalization.CultureInfo.GetCultureInfo("ur-PK").DateTimeFormat);
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("EmployeeId", typeof(int)), new DataColumn("Date", typeof(string)) });
    for (int j = 1; j <= 2; j++)
    {
        for (DateTime i = date1; i <= date2; i = i.AddDays(1))
        {
            dt.Rows.Add(j, i.ToString("dd/MM/yyyy"));
        }
    }
    this.gvEmployees.DataSource = dt;
    this.gvEmployees.DataBind();
}

VB.Net

Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
    Dim dateString As String = TextBox1.Text
    Dim date1 As DateTime = Convert.ToDateTime(dateString, System.Globalization.CultureInfo.GetCultureInfo("ur-PK").DateTimeFormat)
    Dim dateString2 As String = TextBox2.Text
    Dim date2 As DateTime = Convert.ToDateTime(dateString2, System.Globalization.CultureInfo.GetCultureInfo("ur-PK").DateTimeFormat)
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("EmployeeId", GetType(Integer)), New DataColumn("Date", GetType(String))})
    For j As Integer = 1 To 2
        Dim i As DateTime = date1
        While i <= date2
            dt.Rows.Add(j, i.ToString("dd/MM/yyyy"))
            i = i.AddDays(1)
        End While
    Next
    Me.gvEmployees.DataSource = dt
    Me.gvEmployees.DataBind()
End Sub

Screeshot