0

嗨,我如何在新列中显示计算的超时和时间

假设我的数据网格视图中有此列数据:

Emplyee ID | EmployeeName | Date       | TimeIN | TimeOut
        0  | Danilo       | 2013-06-06 | 08:00  | 15:00

然后我想显示这样的结果:

Emplyee ID | EmployeeName | Date       | TimeIN | TimeOut | TOTAL
        0  | Danilo       | 2013-06-06 | 08:00  | 15:00   | 7:00

我试过这个代码:

    Dim sDateFrom As TimeSpan
    Dim sDateTo As TimeSpan
    Dim timeDiff As TimeSpan

    For i As Integer = 0 To DataGridView1.RowCount - 1
        If DataGridView1.Rows(i).Cells(3).Value = lblholdname.Text Then

            sDateFrom = DataGridView1.Rows(i).Cells(3).Value
            sDateTo = DataGridView1.Rows(i).Cells(4).Value

            timeDiff = sDateTo - sDateFrom

'this code is not right i want column instead of row but its not accepting timespan
            DataGridView1.Rows(i).Cells(5).Value = timeDiff
'i try using this too 
'DataGridView1.Columns.Add("TOTAL", "TOTAL") = timeDiff


        End If

    Next

好的,所以我设法得到了我需要的东西,但是出现了一个新问题,而不是显示 1 列,而是显示 2 列,我的表现在看起来像这样

Emplyee ID | EmployeeName | Date       | TimeIN | TimeOut | TOTAL | TOTAL
        0  | Danilo       | 2013-06-06 | 08:00  | 15:00   | 7:00  |

我添加此代码以获得该结果:

    DataGridView1.Columns.Add("TOTAL", "TOTAL")

            DataGridView1.Rows(i).Cells(6).Value = timeDiff

好的,这就是我在数据库中使用 DATA 填充 DGV 的方式:

con = New MySqlConnection
    con.ConnectionString = "server=localhost;username=root;password=nhhs;port=3306;database=employeedb"
    con.Open()
    cmd = New MySqlCommand("select e.employeeID, e.employeefname, e.employeelname, t.dateoftime, t.timein,t.timeout from tblemployee as e, tbltimepunch as t where e.employeelname = '" & lblholdlastname.Text & _
                           "' and e.employeeID = t.employeeID and dateoftime between '" & MonthCalendar1.SelectionStart.ToString("yyyy-MM-dd") & _
                           "' and '" & MonthCalendar1.SelectionEnd.ToString("yyyy-MM-dd") & "'", con)

    da = New MySqlDataAdapter(cmd)
    dt = New DataTable
    da.Fill(dt)

    With DataGridView1
        .Columns(0).DataPropertyName = "employeeID"
        .Columns(1).DataPropertyName = "employeefname"
        .Columns(2).DataPropertyName = "employeelname"
        .Columns(3).DataPropertyName = "dateoftime"
        .Columns(4).DataPropertyName = "timein"
        .Columns(5).DataPropertyName = "timeout"
        .DataSource = dt
    End With
    con.Close()
4

2 回答 2

1

这个想法是为 GridView 创建一个 DataTable,其中包含您在 DAL 或 BL 级别(无论您正在使用它)需要的所有列,而不是在 UI 级别操作 Grid 结构。

Public Function DataTable1() as DataTable
      con = New MySqlConnection
      con.ConnectionString = "server=localhost;username=root;password=nhhs;port=3306;database=employeedb"
      con.Open()
      cmd = New MySqlCommand("select e.employeeID, e.employeefname, e.employeelname, t.dateoftime, t.timein,t.timeout from tblemployee as e, tbltimepunch as t where e.employeelname = '" & lblholdlastname.Text & _
                                   "' and e.employeeID = t.employeeID and dateoftime between '" & MonthCalendar1.SelectionStart.ToString("yyyy-MM-dd") & _
                                   "' and '" & MonthCalendar1.SelectionEnd.ToString("yyyy-MM-dd") & "'", con)

       da = New MySqlDataAdapter(cmd)
       dt = New DataTable
       da.Fill(dt)

       return dt
End Function

创建新的 DataTable 并根据需要手动生成列。您将在此处创建用于添加要添加的任何列的列。

Public Shared Function DataTable2(byval table as DataTable) As DataTable
         Dim tbl As New DataTable()
         tbl.Columns.Add("Col1", GetType(String))
         tbl.Columns.Add("Col2", GetType(String))
         tbl.Columns.Add("Col3", GetType(String))
         For Each row As DataRow In dtDataTable.Rows
               tbl.Rows.Add(New Object() {[String].Format("Col1{0}", row.Item("column1")), [String].Format("Col2{0}", row.Item("column2")), [String].Format("Col3{0}", row.Item("column1") + row.Item("column2"))})
        Next row

    return tbl;
End Function

只需将此对象绑定到 Gridview。

DataGridView1.DataSource = DataTable2(DataTable1);

您将需要根据需要修改和格式化代码。

于 2013-07-05T20:09:18.590 回答
0
var totalColum = new DataGridViewColumn();
totalColum.Name = "Missing Time";
totalColum.CellTemplate = new DataGridViewTextBoxCell();
dataGridView2.Columns.Insert(index, totalColum);
于 2013-07-05T20:13:51.640 回答