Auto generate ASP.Net GridView Rows based on two Date range using C# and VB.Net

Last Reply one month ago By pandeyism

Posted one month ago

i want define limit of rows in gridview for auto generate.

i want that if there is five employee and i give range of two days from date 12/10/2018 to 13/10/2018 then

employee id   Date

1                  12/10/2018 

2                  12/10/2018 

3                  12/10/2018 

4                  12/10/2018 

5                  12/10/2018 


same  for 13/10/2018

Employee ID     Date

1                     13/10/2018 

2                     13/10/2018 

3                     13/10/2018 

4                     13/10/2018 

5                     13/10/2018  

in a gridview, mean that each id repeate as per date given range

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hey akhter,

Please refer below sample.


<asp:GridView runat="server" ID="gvEmployees" AutoGenerateColumns="false">
        <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" />
        <asp:BoundField DataField="Date" HeaderText="Date" />



using System.Data;


Imports System.Data



protected void Page_Load(object sender, EventArgs e)
    DateTime date1 = Convert.ToDateTime("10/12/2018");
    DateTime date2 = Convert.ToDateTime("10/13/2018");
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("EmployeeId", typeof(int)), new DataColumn("Date", typeof(string)) });
    for (DateTime i = date1; i <= date2; i = i.AddDays(1))
        for (int j = 1; j <= 5; j++)
            dt.Rows.Add(j, i.ToShortDateString());
    this.gvEmployees.DataSource = dt;


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim date1 As DateTime = Convert.ToDateTime("10/12/2018")
    Dim date2 As DateTime = Convert.ToDateTime("10/13/2018")
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("EmployeeId", GetType(Integer)), New DataColumn("Date", GetType(String))})
    Dim i As DateTime = date1
    While i <= date2
        For j As Integer = 1 To 5
            dt.Rows.Add(j, i.ToShortDateString())
        i = i.AddDays(1)
    End While
    Me.gvEmployees.DataSource = dt
End Sub