如何对数据网格视图中的时间跨度值求和
how to SUM the timespan values from a datagridview
我想对 gridview 中的值求和,但它是一个时间跨度值。
我想从table计算两个值的总持续时间,即start_time
和end_time
都是[=37=中的列] 和网格视图。
必须将持续时间值添加到 gridview 的下一列。通过我下面的代码,它工作正常。
然后必须将 gridview 上的所有 total_hours 列相加并显示在文本框上
例如,下面是我的网格视图看起来:
Days Start_Time End_Time Total_Hours
--------------------------------------------
day1 10:00AM 07:00PM 09:00:00
day2 10:00AM 08:00PM 10:00:00
上面的tablel是一个Gridview。我想总结总小时数并显示在文本框上
我尝试了以下代码:
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
不需要这个块:
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
改为将持续时间添加到 span3:
span3.Add (duration)
然后在循环结束的外面:
txttotalwork.Text = span3.ToString
您的 while 循环中已经有了 duration
变量,我假设它是 TimeSpan
类型并且已正确填充。
所以您所要做的就是将代码添加到循环中以求和:
'before the loop:
Dim TotalDuration = new Timespan(0)
'inside the loop:
TotalDuration = TotalDuration.Add(duration)
'after the loop:
txttotalwork.Text = TotalDuration.ToString()
我想对 gridview 中的值求和,但它是一个时间跨度值。
我想从table计算两个值的总持续时间,即
start_time
和end_time
都是[=37=中的列] 和网格视图。必须将持续时间值添加到 gridview 的下一列。通过我下面的代码,它工作正常。
然后必须将 gridview 上的所有 total_hours 列相加并显示在文本框上
例如,下面是我的网格视图看起来:
Days Start_Time End_Time Total_Hours
--------------------------------------------
day1 10:00AM 07:00PM 09:00:00
day2 10:00AM 08:00PM 10:00:00
上面的tablel是一个Gridview。我想总结总小时数并显示在文本框上
我尝试了以下代码:
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
不需要这个块:
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
改为将持续时间添加到 span3:
span3.Add (duration)
然后在循环结束的外面:
txttotalwork.Text = span3.ToString
您的 while 循环中已经有了 duration
变量,我假设它是 TimeSpan
类型并且已正确填充。
所以您所要做的就是将代码添加到循环中以求和:
'before the loop:
Dim TotalDuration = new Timespan(0)
'inside the loop:
TotalDuration = TotalDuration.Add(duration)
'after the loop:
txttotalwork.Text = TotalDuration.ToString()