Hi moayyad,
Please refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvCustomers_OnRowDataBound">
<Columns>
<asp:BoundField DataField="Current_Date" HeaderText="Current_Date" />
<asp:BoundField DataField="End_Date" HeaderText="End_Date" />
</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("Current_Date", typeof(DateTime)), new DataColumn("End_Date", typeof(DateTime)), new DataColumn("totaldiff", typeof(string)) });
dt.Rows.Add(DateTime.Now, "11/01/2018");
dt.Rows.Add(DateTime.Now, "11/02/2018");
dt.Rows.Add(DateTime.Now, "11/03/2018");
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
protected void gvCustomers_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TableCell cell = new TableCell();
cell.Text = "<b>Difference</b>";
e.Row.Cells.AddAt(gvCustomers.Columns.Count, cell);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime currentdate = Convert.ToDateTime(e.Row.Cells[0].Text);
int currenthour = currentdate.Hour;
int currentsecond = currentdate.Second;
int currentminute = currentdate.Minute;
DateTime enddate = Convert.ToDateTime(e.Row.Cells[1].Text);
int day1 = currentdate.Day;
int day2 = enddate.Day;
int totalday = day1 - day2;
int endhour = enddate.Hour;
int endminute = enddate.Minute;
int endsecond = enddate.Second;
int diffhour = currenthour - endhour;
int diffminute = currentminute - endminute;
int diffsecond = currentsecond - endsecond;
string totaldiff = totalday + ":" + diffhour + ":" + diffminute + ":" + diffsecond;
TableCell cell = new TableCell();
cell.Text = totaldiff;
e.Row.Cells.AddAt(gvCustomers.Columns.Count, cell);
}
}
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("Current_Date", GetType(DateTime)), New DataColumn("End_Date", GetType(DateTime)), New DataColumn("totaldiff", GetType(String))})
dt.Rows.Add(DateTime.Now, "11/01/2018")
dt.Rows.Add(DateTime.Now, "11/02/2018")
dt.Rows.Add(DateTime.Now, "11/03/2018")
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End If
End Sub
Protected Sub gvCustomers_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles Me.Load
If e.Row.RowType = DataControlRowType.Header Then
Dim cell As TableCell = New TableCell()
cell.Text = "<b>Difference</b>"
e.Row.Cells.AddAt(gvCustomers.Columns.Count, cell)
End If
If e.Row.RowType = DataControlRowType.DataRow Then
Dim currentdate As DateTime = Convert.ToDateTime(e.Row.Cells(0).Text)
Dim currenthour As Integer = currentdate.Hour
Dim currentsecond As Integer = currentdate.Second
Dim currentminute As Integer = currentdate.Minute
Dim enddate As DateTime = Convert.ToDateTime(e.Row.Cells(1).Text)
Dim day1 As Integer = currentdate.Day
Dim day2 As Integer = enddate.Day
Dim totalday As Integer = day1 - day2
Dim endhour As Integer = enddate.Hour
Dim endminute As Integer = enddate.Minute
Dim endsecond As Integer = enddate.Second
Dim diffhour As Integer = currenthour - endhour
Dim diffminute As Integer = currentminute - endminute
Dim diffsecond As Integer = currentsecond - endsecond
Dim totaldiff As String = totalday & ":" & diffhour & ":" & diffminute & ":" & diffsecond
Dim cell As TableCell = New TableCell()
cell.Text = totaldiff
e.Row.Cells.AddAt(gvCustomers.Columns.Count, cell)
End If
End Sub
Screenshot
