2013-07-05 31 views
0

嗨,我如何能在一個新的列中顯示計算超時和timein如何使用datagridview中其他2列的總和來添加新列?

說我在數據網格視圖中有這樣的列數據:

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 

好吧,這是我如何填充我的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() 
+0

你是如何填充你的數據網格?數據綁定了嗎? – Bill

+0

是啊我的datagridview加載我的數據庫中的數據我以某種方式設法做我想要的,但我的下一個問題是它添加2列而不是1 – daniloJR

+0

請張貼代碼如何綁定當前的網格視圖。你使用DataSet或DataTable填充? – Learner

回答

1

的想法是創建一個你需要在DAL或BL水平(但你是所有列的DataTable使用它)爲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); 

您需要根據需要修改和格式化代碼。

+0

感謝問題解決=) – daniloJR

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