【问题标题】:vb.net update table with datagridviewvb.net 使用 datagridview 更新表
【发布时间】:2017-09-16 12:53:33
【问题描述】:

我堆叠在我的代码中,我有一个 datagridview,然后如果 datagridview 值在我的表中已准备好,则使用 datagridview 值更新我的表,否则插入我的表中。但是我在更新时堆叠,然后只更新一次,当值不在我的表中时插入到我的表中失败。

这是我的表格和值

这是我的代码

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 日”日期 2 更新为当我开始新的日期 1 和日期 2 时,所有项目都将重复正常,体积、数量、体积 * 数量loss stuff date1 和 date2 的值不同

ex 插入新数据 我的查询没有插入到我的表中

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

我的代码没有返回我想要的那两个图像。 请指路,谢谢

【问题讨论】:

  • 您收到什么错误?这个例子不是很简洁。
  • 嗨@DanielShillcock,对不起。我会编辑这篇文章,请指路谢谢

标签: vb.net datagridview dataset


【解决方案1】:

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

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 & "'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多