Hi kankon,
Please refer below sample.
Note: For this sample i have used temporary DataTable. For more details refer Dynamically create DataTable and bind to GridView in ASP.Net.
HTML
<table>
<tr>
<td>
<asp:TextBox ID="txtHours" runat="server" Text="12:10:00"></asp:TextBox>
</td>
</tr>
</table>
<br />
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<asp:BoundField DataField="ID" HeaderText="Id" />
<asp:BoundField DataField="Days" HeaderText="Days" FooterText="TotalTime :" />
<asp:TemplateField HeaderText="Time">
<ItemTemplate>
<asp:Label ID="lblTime" runat="server" Text='<%#Eval("Time") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotalTime" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbDetails" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespace
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[3]{
new DataColumn("ID"),
new DataColumn("Days"),
new DataColumn("Time")
});
dt.Rows.Add(1, "Monday", "02:20:00");
dt.Rows.Add(2, "Tuesday", "01:10:00");
dt.Rows.Add(3, "Wednesday", "03:30:00");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
protected void OnCheckedChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as CheckBox).NamingContainer as GridViewRow;
DateTime dt = new DateTime();
foreach (GridViewRow gvRow in gvDetails.Rows)
{
CheckBox cb = gvRow.FindControl("cbDetails") as CheckBox;
if (cb.Checked)
{
DateTime time = Convert.ToDateTime((gvRow.FindControl("lblTime") as Label).Text);
dt = dt.Add(time.TimeOfDay);
}
}
TimeSpan time1 = Convert.ToDateTime(txtHours.Text).Subtract(dt);
(gvDetails.FooterRow.FindControl("lblTotalTime") as Label).Text = string.Format("{0:D2}:{1:D2}:{2:D2}", time1.Hours, time1.Minutes, time1.Seconds);
}
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(2) {New DataColumn("ID"), New DataColumn("Days"), New DataColumn("Time")})
dt.Rows.Add(1, "Monday", "02:20:00")
dt.Rows.Add(2, "Tuesday", "01:10:00")
dt.Rows.Add(3, "Wednesday", "03:30:00")
gvDetails.DataSource = dt
gvDetails.DataBind()
End If
End Sub
Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, CheckBox)).NamingContainer, GridViewRow)
Dim dt As DateTime = New DateTime()
For Each gvRow As GridViewRow In gvDetails.Rows
Dim cb As CheckBox = TryCast(gvRow.FindControl("cbDetails"), CheckBox)
If cb.Checked Then
Dim time As DateTime = Convert.ToDateTime((TryCast(gvRow.FindControl("lblTime"), Label)).Text)
dt = dt.Add(time.TimeOfDay)
End If
Next
Dim time1 As TimeSpan = Convert.ToDateTime(txtHours.Text).Subtract(dt)
TryCast(gvDetails.FooterRow.FindControl("lblTotalTime"), Label).Text = String.Format("{0:D2}:{1:D2}:{2:D2}", time1.Hours, time1.Minutes, time1.Seconds)
End Sub
Screenshot