嗨,我如何能在一個新的列中顯示計算超時和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()
你是如何填充你的數據網格?數據綁定了嗎? – Bill
是啊我的datagridview加載我的數據庫中的數據我以某種方式設法做我想要的,但我的下一個問題是它添加2列而不是1 – daniloJR
請張貼代碼如何綁定當前的網格視圖。你使用DataSet或DataTable填充? – Learner