I want to sum the values that are in gridview, but it is a timespan value.
I want to calculate the total duration of two values from a table, that is
start_time
andend_time
are the column in both table and grid view.The duration value must be added to next column in gridview. It works fine by my code below.
Then the all the total_hours column on gridview must be summed and showed on a textbox
For example, the following is my grid view looks:
Days Start_Time End_Time Total_Hours
--------------------------------------------
day1 10:00AM 07:00PM 09:00:00
day2 10:00AM 08:00PM 10:00:00
The above tablel is a Gridview. I want to sum the total hours and display on a textbox
I tried the following code:
If dr.HasRows Then
While dr.Read
Dim s_hour = dr("start_time")
Dim e_hour = dr("end_time")
Dim duration As TimeSpan = e_hour - s_hour
DataGridView1.Rows.Add()
DataGridView1.Item(dgv_sno.Name, DataGridView1.Rows.Count - 1).Value = DataGridView1.Rows.Count
DataGridView1.Item(dgv_personid.Name, DataGridView1.Rows.Count - 1).Value = IIf(IsDBNull(dr("PersonID")), 0, dr("PersonID"))
DataGridView1.Item(dgv_person.Name, DataGridView1.Rows.Count - 1).Value = IIf(IsDBNull(dr("name")), 0, dr("name"))
DataGridView1.Item(dgv_company.Name, DataGridView1.Rows.Count - 1).Value = IIf(IsDBNull(dr("CompanyName")), 0, dr("CompanyName"))
DataGridView1.Item(dgv_project.Name, DataGridView1.Rows.Count - 1).Value = IIf(IsDBNull(dr("proj_Name")), 0, dr("proj_Name"))
DataGridView1.Item(dgv_tasks.Name, DataGridView1.Rows.Count - 1).Value = IIf(IsDBNull(dr("task")), 0, dr("task"))
DataGridView1.Item(dgv_status.Name, DataGridView1.Rows.Count - 1).Value = IIf(IsDBNull(dr("status")), 0, dr("status"))
DataGridView1.Item(dgv_date.Name, DataGridView1.Rows.Count - 1).Value = IIf(IsDBNull(dr("Record_Date")), 0, dr("Record_Date"))
DataGridView1.Item(dgv_starttime.Name, DataGridView1.Rows.Count - 1).Value = IIf(IsDBNull(dr("start_time")), 0, dr("start_time"))
DataGridView1.Item(dgv_endtime.Name, DataGridView1.Rows.Count - 1).Value = IIf(IsDBNull(dr("end_time")), 0, dr("end_time"))
DataGridView1.Item(dgv_hrs.Name, DataGridView1.Rows.Count - 1).Value = duration
If Val(DataGridView1.Item(dgv_hrs.Name, DataGridView1.Rows.Count - 1).Value) >= 1 Then
span1 = dr("start_time")
span2 = dr("end_time")
Dim span3 As TimeSpan = span1.Add(span2)
txttotalwork.Text = span3.ToString
Else
?
End If
End While
End If