Calculate Day and Hour difference between to DateTime in ASP.Net GridView using C# and VB.Net

Last Reply 27 days ago By pandeyism

Posted 27 days ago

I have gridview in which i have set condition for calculating hours,Day on the basis of INtime and Outtime on page load.

but it working fine when i change value on outime then it initiate calculation but i want to calculate on page load

i have gridview in which i have two column in which have INTIME and OUT Time

INTIME OUTTIME Hour
11/11/2018 8:00:00 AM 11/11/2018 8:00:00 AM  

Now i want when i load this page then it auto calculate hours and day

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                var total = objdt.AsEnumerable().Select(c => new { OT = c.Field<int>("OT") }).Sum(m => m.OT);
                var total1 = objdt.AsEnumerable().Select(c => new { Days = c.Field<int>("Days") }).Sum(m => m.Days);
                Label lblTotal = (Label)e.Row.FindControl("lblGrandtotal");
                Label lblTotal1 = (Label)e.Row.FindControl("lblGrandtotal1");
                lblTotal.Text = total.ToString();
                lblTotal1.Text = total1.ToString();
            }
        }

 

Posted 27 days ago

Hey akhter,

Please refer below sample

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="CustomerID">
            <ItemTemplate>
                <asp:Label ID="customerid" runat="server" Text='<%# Bind("customerid") %>'> </asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="txtName" runat="server" Text='<%# Bind("Name") %>'> </asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="EntryDate">
            <ItemTemplate>
                <asp:TextBox ID="txtEntryDate" AutoPostBack="true" runat="server" Text='<%# Bind("EntryDate") %>'
                    DataFormatString="{0:dd-MMM-yyyy h:mm:ss tt}"> </asp:TextBox></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="EndDate">
            <ItemTemplate>
                <asp:TextBox ID="txtEndDate" AutoPostBack="true" runat="server" Text='<%# Bind("EndDate") %>'
                    DataFormatString="{0:dd-MMM-yyyy h:mm:ss tt}"> </asp:TextBox></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Hours">
            <ItemTemplate>
                <asp:TextBox ID="txtHours" runat="server" AutoPostBack="true" Text='<%# Bind("Hours") %>'> </asp:TextBox></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Days">
            <ItemTemplate>
                <asp:TextBox ID="txtDays" runat="server" Text='<%# Bind("Days") %>'> </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { 
                            new DataColumn("customerid", typeof(int)), 
                            new DataColumn("Name", typeof(string)),
                            new DataColumn("EntryDate", typeof(DateTime)), 
                            new DataColumn("EndDate", typeof(DateTime)), 
                            new DataColumn("Hours", typeof(int)) ,
                            new DataColumn("Days", typeof(int))});
        dt.Rows.Add(1, "mudassar khan", "12/20/2018 10:00:00", "12/26/2018 05:00:00 PM");
        dt.Rows.Add(2, "Maria", "12/25/2018 11:59:00", "12/26/2018 07:00:00 PM");
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox txtEntryDate = e.Row.FindControl("txtEntryDate") as TextBox;
        TextBox txtEndDate = e.Row.FindControl("txtEndDate") as TextBox;
        DateTime startDate = Convert.ToDateTime(txtEntryDate.Text);
        DateTime endDate = Convert.ToDateTime(txtEndDate.Text);
        TextBox txtHours = e.Row.FindControl("txtHours") as TextBox;
        TextBox txtDays = e.Row.FindControl("txtDays") as TextBox;
        txtHours.Text = endDate.Subtract(startDate).Hours.ToString();
        txtDays.Text = endDate.Subtract(startDate).Days.ToString();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("customerid", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("EntryDate", GetType(DateTime)), New DataColumn("EndDate", GetType(DateTime)), New DataColumn("Hours", GetType(Integer)), New DataColumn("Days", GetType(Integer))})
        dt.Rows.Add(1, "mudassar khan", "12/20/2018 10:00:00", "12/26/2018 05:00:00 PM")
        dt.Rows.Add(2, "Maria", "12/25/2018 11:59:00", "12/26/2018 07:00:00 PM")
        Me.GridView1.DataSource = dt
        Me.GridView1.DataBind()
    End If
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim txtEntryDate As TextBox = TryCast(e.Row.FindControl("txtEntryDate"), TextBox)
        Dim txtEndDate As TextBox = TryCast(e.Row.FindControl("txtEndDate"), TextBox)
        Dim startDate As DateTime = Convert.ToDateTime(txtEntryDate.Text)
        Dim endDate As DateTime = Convert.ToDateTime(txtEndDate.Text)
        Dim txtHours As TextBox = TryCast(e.Row.FindControl("txtHours"), TextBox)
        Dim txtDays As TextBox = TryCast(e.Row.FindControl("txtDays"), TextBox)
        txtHours.Text = endDate.Subtract(startDate).Hours.ToString()
        txtDays.Text = endDate.Subtract(startDate).Days.ToString()
    End If
End Sub

Screenshot