如何从datagridview中计算时间跨度值

时间:2015-07-23 05:25:27

标签: sql-server vb.net datagridview timespan

我想对gridview中的值求和,但它是一个时间跨度值。

  1. 我想计算表中两个值的总持续时间,start_timeend_time是表格和网格视图中的列。

  2. 必须将持续时间值添加到gridview中的下一列。我的代码可以正常工作。

  3. 然后必须将gridview上的所有total_hours列相加并显示在文本框中

  4. 例如,以下是我的网格视图:

    Days    Start_Time  End_Time    Total_Hours  
    --------------------------------------------
    day1    10:00AM     07:00PM     09:00:00
    day2    10:00AM     08:00PM     10:00:00
    

    上面的表格是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
    

2 个答案:

答案 0 :(得分:0)

不需要这个块:

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

答案 1 :(得分:0)

你的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()