如何使用datagridview中另外2列的总和添加新列?

时间:2013-07-05 19:28:13

标签: .net vb.net

您如何在新列中显示计算的超时和timein

说我在数据网格视图中有这个列数据:

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

2 个答案:

答案 0 :(得分:1)

我们的想法是创建一个DataTable,其中包含您在DAL或BL级别所需的所有列(但是您正在使用它)用于GridView,而不是在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);

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

答案 1 :(得分:0)

var totalColum = new DataGridViewColumn();
totalColum.Name = "Missing Time";
totalColum.CellTemplate = new DataGridViewTextBoxCell();
dataGridView2.Columns.Insert(index, totalColum);