ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Calculate Day and Hour difference between to DateTime in ASP.Net GridView using C# and VB.Net<p>Hey <a class="username" rel="akhter"> akhter</a>,</p> <p>Please refer below sample</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; OnRowDataBound=&#34;GridView1_RowDataBound&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;CustomerID&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;customerid&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Bind(&#34;customerid&#34;) %&gt;&#39;&gt; &lt;/asp:Label&gt;&lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;txtName&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Bind(&#34;Name&#34;) %&gt;&#39;&gt; &lt;/asp:Label&gt;&lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;EntryDate&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtEntryDate&#34; AutoPostBack=&#34;true&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Bind(&#34;EntryDate&#34;) %&gt;&#39; DataFormatString=&#34;{0:dd-MMM-yyyy h:mm:ss tt}&#34;&gt; &lt;/asp:TextBox&gt;&lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;EndDate&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtEndDate&#34; AutoPostBack=&#34;true&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Bind(&#34;EndDate&#34;) %&gt;&#39; DataFormatString=&#34;{0:dd-MMM-yyyy h:mm:ss tt}&#34;&gt; &lt;/asp:TextBox&gt;&lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Hours&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtHours&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; Text=&#39;&lt;%# Bind(&#34;Hours&#34;) %&gt;&#39;&gt; &lt;/asp:TextBox&gt;&lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Days&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtDays&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Bind(&#34;Days&#34;) %&gt;&#39;&gt; &lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;customerid&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;EntryDate&#34;, typeof(DateTime)), new DataColumn(&#34;EndDate&#34;, typeof(DateTime)), new DataColumn(&#34;Hours&#34;, typeof(int)) , new DataColumn(&#34;Days&#34;, typeof(int))}); dt.Rows.Add(1, &#34;mudassar khan&#34;, &#34;12/20/2018 10:00:00&#34;, &#34;12/26/2018 05:00:00 PM&#34;); dt.Rows.Add(2, &#34;Maria&#34;, &#34;12/25/2018 11:59:00&#34;, &#34;12/26/2018 07:00:00 PM&#34;); 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(&#34;txtEntryDate&#34;) as TextBox; TextBox txtEndDate = e.Row.FindControl(&#34;txtEndDate&#34;) as TextBox; DateTime startDate = Convert.ToDateTime(txtEntryDate.Text); DateTime endDate = Convert.ToDateTime(txtEndDate.Text); TextBox txtHours = e.Row.FindControl(&#34;txtHours&#34;) as TextBox; TextBox txtDays = e.Row.FindControl(&#34;txtDays&#34;) as TextBox; txtHours.Text = endDate.Subtract(startDate).Hours.ToString(); txtDays.Text = endDate.Subtract(startDate).Days.ToString(); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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(&#34;customerid&#34;, GetType(Integer)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;EntryDate&#34;, GetType(DateTime)), New DataColumn(&#34;EndDate&#34;, GetType(DateTime)), New DataColumn(&#34;Hours&#34;, GetType(Integer)), New DataColumn(&#34;Days&#34;, GetType(Integer))}) dt.Rows.Add(1, &#34;mudassar khan&#34;, &#34;12/20/2018 10:00:00&#34;, &#34;12/26/2018 05:00:00 PM&#34;) dt.Rows.Add(2, &#34;Maria&#34;, &#34;12/25/2018 11:59:00&#34;, &#34;12/26/2018 07:00:00 PM&#34;) 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(&#34;txtEntryDate&#34;), TextBox) Dim txtEndDate As TextBox = TryCast(e.Row.FindControl(&#34;txtEndDate&#34;), 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(&#34;txtHours&#34;), TextBox) Dim txtDays As TextBox = TryCast(e.Row.FindControl(&#34;txtDays&#34;), TextBox) txtHours.Text = endDate.Subtract(startDate).Hours.ToString() txtDays.Text = endDate.Subtract(startDate).Days.ToString() End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/Le5zVCD.jpg" alt="" width="468" height="159" /></p>https://www.aspforums.net:443/Threads/188839/Calculate-Day-and-Hour-difference-between-to-DateTime-in-ASPNet-GridView-using-C-and-VBNet/https://www.aspforums.net:443/Threads/188839/Calculate-Day-and-Hour-difference-between-to-DateTime-in-ASPNet-GridView-using-C-and-VBNet/Thu, 27 Dec 2018 04:23:15 GMT