Hi Alladin,
I have created one sample please check below.
HTML
<div>
<asp:GridView runat="server" ID="grdAttendance" AutoGenerateColumns="false" OnRowDataBound="grdAttendance_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="CheckIn" HeaderText="CheckIn" />
<asp:BoundField DataField="CheckOut" HeaderText="CheckOut" />
<asp:TemplateField HeaderText="Outdoor Hours">
<ItemTemplate>
<asp:Label runat="server" ID="lblOutdoorHours"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("ID", typeof(int)),
new DataColumn("CheckIn", typeof(string)),
new DataColumn("CheckOut", typeof(string))
});
dt.Rows.Add(1, DateTime.Now, DateTime.Now.AddHours(2));
dt.Rows.Add(2, DateTime.Now.AddHours(4), DateTime.Now.AddHours(7));
dt.Rows.Add(3, DateTime.Now.AddHours(10), DateTime.Now.AddHours(14));
dt.Rows.Add(4, DateTime.Now.AddHours(19), DateTime.Now.AddHours(18));
dt.Rows.Add(5, DateTime.Now.AddHours(25), DateTime.Now.AddHours(29));
grdAttendance.DataSource = dt;
grdAttendance.DataBind();
}
}
protected void grdAttendance_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int current = 0; current < grdAttendance.Rows.Count; current++)
{
string CheckOut = grdAttendance.Rows[current].Cells[2].Text;
for (int next = current + 1; next < grdAttendance.Rows.Count; next++)
{
string nextCheckIn = grdAttendance.Rows[current + 1].Cells[1].Text;
TimeSpan duration = DateTime.Parse(nextCheckIn.ToString()).Subtract(DateTime.Parse(CheckOut.ToString()));
grdAttendance.Rows[current + 1].Cells[3].Text = duration.ToString();
break;
}
}
}
Screenshot
