Hi democloud,
Check the below code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Dates" HeaderText="Date" />
<asp:BoundField DataField="Day" HeaderText="Day" />
<asp:TemplateField HeaderText="Normal">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtNormalHours" Text='<%#Eval("NormalHours") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weekend">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtWeekendHours" Text='<%#Eval("WeekendHours") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Insert" runat="server" OnClick="Insert" />
<br />
<br />
<asp:TextBox runat="server" ID="txtTotal" />
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] {
new System.Data.DataColumn("Dates", typeof(string)),
new System.Data.DataColumn("Day", typeof(string)),
new System.Data.DataColumn("NormalHours",typeof(string)),
new System.Data.DataColumn("WeekendHours",typeof(string)),
new System.Data.DataColumn("Time",typeof(string)) });
dt.Rows.Add("11/04/2019", "Monday", "03:00:00", "4.5", "12:00pm 03:00pm");
dt.Rows.Add("11/04/2019", "Monday", "00:30:00", "5.5", "09:30am 10:00am");
dt.Rows.Add("11/11/2019", "Monday", "03:00:00", "4", "12:00pm 03:00pm");
dt.Rows.Add("11/11/2019", "Monday", "00:30:00", "2", "09:30am 10:00am");
dt.Rows.Add("11/11/2019", "Monday", "06:00:00", "0", "01:00am 06:00am");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Insert(object sender, EventArgs e)
{
TimeSpan new_normal = new TimeSpan();
foreach (GridViewRow row in GridView1.Rows)
{
TextBox normal = (TextBox)(row.FindControl("txtNormalHours"));
TextBox weekend = (TextBox)(row.FindControl("txtWeekendHours"));
TimeSpan normal_hours = TimeSpan.Parse(normal.Text) + TimeSpan.FromHours(Convert.ToDouble(weekend.Text));
new_normal = new_normal + normal_hours;
}
txtTotal.Text = ((int)new_normal.TotalHours).ToString().PadLeft(2, '0') + ":" + new_normal.Minutes.ToString().PadLeft(2, '0');
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As Data.DataTable = New Data.DataTable()
dt.Columns.AddRange(New Data.DataColumn() {New Data.DataColumn("Dates", GetType(String)),
New Data.DataColumn("Day", GetType(String)),
New Data.DataColumn("NormalHours", GetType(String)),
New Data.DataColumn("WeekendHours", GetType(String)),
New Data.DataColumn("Time", GetType(String))})
dt.Rows.Add("11/04/2019", "Monday", "03:00:00", "4.5", "12:00pm 03:00pm")
dt.Rows.Add("11/04/2019", "Monday", "00:30:00", "5.5", "09:30am 10:00am")
dt.Rows.Add("11/11/2019", "Monday", "03:00:00", "4", "12:00pm 03:00pm")
dt.Rows.Add("11/11/2019", "Monday", "00:30:00", "2", "09:30am 10:00am")
dt.Rows.Add("11/11/2019", "Monday", "06:00:00", "0", "01:00am 06:00am")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim new_normal As TimeSpan = New TimeSpan()
For Each row As GridViewRow In GridView1.Rows
Dim normal As TextBox = CType((row.FindControl("txtNormalHours")), TextBox)
Dim weekend As TextBox = CType((row.FindControl("txtWeekendHours")), TextBox)
Dim normal_hours As TimeSpan = TimeSpan.Parse(normal.Text) + TimeSpan.FromHours(Convert.ToDouble(weekend.Text))
new_normal = new_normal + normal_hours
Next
txtTotal.Text = (CInt(new_normal.TotalHours)).ToString().PadLeft(2, "0"c) & ":" + new_normal.Minutes.ToString().PadLeft(2, "0"c)
End Sub
Screenshot