2017-04-20 92 views
0

我堆栈在我的代码我有一个datagridview然后如果datagridview值已准备好在我的表然后更新我的表与datagridview值否则插入到我的表。但是,当我更新的时候,我只是一次将更新的值插入到表中,当值不在我的表中时失败。vb.net与datagridview更新表

这里是我的表,价值 enter image description here

这里我的代码

For Each row As DataGridViewRow In dgvLosshours.Rows 
If (row.Cells("losshrs").Value = "") Then 
    MsgBox("Losshours can't Empty", MsgBoxStyle.Critical) 
    Return 
Else 
    Dim i As Integer = 0 
    Dim hd As Integer = row.Cells("headcount").Value * row.Cells("workhrs").Value - row.Cells("losshrs").Value 
    Dim cmd1 As New MySqlCommand 
    Dim mydt1 As New DataSet 
    Dim myadapt1 As New MySqlDataAdapter 
    openConnectionsMySQL() 
    cmd1 = New MySqlCommand("select * from whweek where periode = date_format(NOW(), '%m-%Y') ", connectMySQL) 
    myadapt1 = New MySqlDataAdapter(cmd1) 
    myadapt1.Fill(mydt1, "list") 
    connectMySQL.Dispose() 
    If (mydt1.Tables(0).Rows.Count = 19) Then 
     Dim c As String = "Update whweek losshours : " & row.Cells("losshrs").Value & ", RegularHours :" & hd 
      With dgvLosshours 
       dt = ctrl.ActionQuery("update whweek set workhrs = '" & row.Cells("workhrs").Value & "', losshours = '" & _ 
       row.Cells("losshrs").Value & "', reghrs = '" & hd & "', Ke = '" & _ 
       row.Cells("edate").Value & "', modifyby = '" & frmMenu.toolempno.Text & _ 
       "', modifyon = '" & today & "' where CodeDept = '" & _ 
       row.Cells("dept").Value & "' and class = '" & row.Cells("class").Value & "' ") 
       dt = ctrl.ActionQuery("INSERT INTO log (EmpNo, Tanggal, Ket) VALUES ('" & frmMenu.toolempno.Text & "', '" & today & "', '" & c & "')") 
      End With 

      MsgBox("LossHours had been updated!") 
      dgvLosshours.Rows.Clear() 

    ElseIf (mydt1.Tables(0).Rows.Count = 0) Then 

     Dim hd1 As Integer = row.Cells("headcount").Value * row.Cells("workhrs").Value - row.Cells("losshrs").Value 
     Dim c As String = "Add LossHrs By :" & frmMenu.toolempno.Text & " Period : " & period 
     dt = ctrl.ActionQuery("insert into whweek(codedept, class, workhrs, losshours, reghrs, Dari, Ke, createby, createon) values('" & _ 
     row.Cells("dept").Value & _ 
     "','" & row.Cells("class").Value & _ 
     "','" & row.Cells("workhrs").Value & _ 
     "','" & row.Cells("losshrs").Value & _ 
     "','" & hd1 & "','" & _ 
     row.Cells("sdate").Value & "', '" & _ 
     row.Cells("edate").Value & "', '" & _ 
     frmMenu.toolempno.Text & "','" & today & "')") 
     dt = ctrl.ActionQuery("INSERT INTO log (EmpNo, Tanggal, Ket) VALUES ('" & _ 
     frmMenu.toolempno.Text & "', '" & today & "', '" & c & "')") 
     MsgBox("Successful Save LossHours") 
     dgvLosshours.Rows.Clear() 
    End If 

End If 

下一页

,我想更新量达到100的所有项目和“2017年4月26日'date2所有项目,当我开始新的日期1和日期2所有项目将复制是好的,音量,数量,音量*数量损失的东西date1和date2不是相同的值

前插入新的数据 我的查询不能插入我的表

enter image description here

更新数据 我的代码只需更新的第一行,我想是所有更新一行。

enter image description here

我的代码不会返回这两个形象,我想。 请给我指路,感谢

+0

你收到了什么错误?这个例子不是很简洁。 –

+0

Hi @DanielShillcock,对不起。我将编辑这篇文章,并请表示感谢 –

回答

0

感谢我知道我的答案在这里的代码:

update whweek set wh= '" & dgvLosshours.Rows(intI).Cells("wh").Value & "', lh= '" & _ 
             dgvLosshours.Rows(intI).Cells("lh").Value & "', reg= '" & hd & "', Ke = '" & _ 
             dgvLosshours.Rows(intI).Cells("edate").Value & "', modifyby = '" & frmMenu.no.Text & _ 
             "', modifyon = '" & today & "', periode = '" & dgvLosshours.Rows(intI).Cells("periode").Value & "' where id= '" & _ 
             dgvLosshours.Rows(intI).Cells("id").Value & "' and class = '" & dgvLosshours.Rows(intI).Cells("class").Value & _ 
             "' and Dari = '" & dateFrom.Text & "'