none
想请教如何让多个checkbox checked的data 更新到database。 RRS feed

  • 问题

  • 我篇写了只能让一个checkbox checked 的data 更新到database。我想请问如何让多个checkbox checked的data 更新到database。

    以下是我的代码

     Dim ID As String
            ID = txtID.Text

            txtID.Text = ID
            If txtID.Text = Nothing Then
                MessageBox.Show("Pls Select Data Correctly", "Information")
            Else
                Product.txtID.Text = Me.txtID.Text
                Textbox_Retrieve_Data()
                Return_Product()

            End If

      Private Sub Textbox_Retrieve_Data()

            Dim constr As String = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
            Using con As OleDbConnection = New OleDbConnection(constr)
                Using cmd As OleDbCommand = New OleDbCommand("SELECT ID, ProductID, ProductName, VendorName, Qty, Price, Description, TotalPrice FROM tbl_Product WHERE ID = " + txtSID.Text)
                    cmd.CommandType = CommandType.Text
                    cmd.Connection = con
                    con.Open()
                    Using sdr As OleDbDataReader = cmd.ExecuteReader()
                        sdr.Read()
                        Product.txtProductID1.Text = sdr("ProductID").ToString
                        Product.txtProductQuantity1.Text = sdr("Qty").ToString()
                        Product.txtProductPrice1.Text = sdr("Price").ToString()
                        Product.txtProductTotalPrice1.Text = sdr("TotalPrice").ToString()
                    End Using
                    con.Close()
                End Using
            End Using

        End Sub

    Private Sub Return_Product()

            Dim con As New OleDbConnection
            Dim cmd As New OleDbCommand
            Try
                con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
                con.Open()
                cmd.Connection = con

                cmd.CommandText = "UPDATE tbl_Product SET ProductID = @ProductID, Qty = @Qty, Price = @Price, TotalPrice = @TotalPrice Where ID = " & txtSID.Text

                cmd.Parameters.AddWithValue("@ProductID", Me.txtShoppingProductID.Text)

                TotalShopping_Cart_Qty()

                cmd.Parameters.AddWithValue("@Qty", Me.txtShoppingCartQty.Text)

                cmd.Parameters.AddWithValue("@Price", Me.txtShoppingCartPrice.Text)

                TotalShopping_Cart_TotalPrice()

                cmd.Parameters.AddWithValue("@TotalPrice", Me.txtTotalShoppingCartPrice.Text)

                cmd.ExecuteNonQuery()


                MessageBox.Show("You data have been cancel successful", "Information")
                Product.btnClear.PerformClick()

            Catch ex As Exception
                MessageBox.Show("Error while cancel record on table..." & ex.Message, "Update Records")
            Finally
                con.Close()
                Product.btnClear.PerformClick()

            End Try

     Private Sub TotalShopping_Cart_Qty()

            Dim ProductQTY As Integer = CInt(Product.txtProductQuantity1.Text)
            Dim ShoppingCartQTY As Integer = CInt(Me.txtShoppingCartQty.Text)

            txtShoppingCartQty.Text = CStr(ProductQTY + ShoppingCartQTY)

        End Sub


        Private Sub TotalShopping_Cart_TotalPrice()

            Dim TotalProductPrice As Double = CDbl(Product.txtProductTotalPrice1.Text)
            Dim TotalShoppingCartPrice As Double = CDbl(Me.txtTotalShoppingCartPrice.Text)

            txtTotalShoppingCartPrice.Text = CStr(TotalProductPrice + TotalShoppingCartPrice)


        End Sub

    请指教谢谢

    2021年2月24日 7:25

答案

  • Hi christing,

    尝试以下代码:

            Dim dic As Dictionary(Of Integer, List(Of Integer)) = New Dictionary(Of Integer, List(Of Integer))
            Dim total_Cart_Qty As Integer = 0
            Dim total_price As Integer
            Dim id As Integer
            Dim isChecked As Boolean = False
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim chk As DataGridViewCheckBoxCell = CType(row.Cells(0), DataGridViewCheckBoxCell)
                If chk.Value = True Then
                    isChecked = True
                    id = Convert.ToInt32(row.Cells("SID").Value)
                    total_Cart_Qty = Convert.ToInt32(row.Cells("Cart_Qty").Value)
                    total_price = Convert.ToInt32(row.Cells("Total_Price").Value)
    
                    If Not dic.Keys.Contains(id) Then
                        dic.Add(id, New List(Of Integer))
                        dic(id).Add(total_Cart_Qty)
                        dic(id).Add(total_price)
                    Else
                        dic(id)(0) = dic(id)(0) + total_Cart_Qty
                        dic(id)(1) = dic(id)(1) + total_price
                    End If
                End If
            Next
            If isChecked Then
                Using con As New OleDbConnection
                    con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
                    con.Open()
                    Dim cmd As New OleDbCommand
                    cmd.Connection = con
                    For Each key In dic.Keys
    
                        cmd.CommandText = "SELECT Qty,TotalPrice FROM tbl_Product Where ID = " & key
                        Dim reader = cmd.ExecuteReader
                        While reader.Read
                            total_Cart_Qty = Convert.ToInt32(reader("Qty"))
                            total_price = Convert.ToInt32(reader("TotalPrice"))
                        End While
                        reader.Close()
    
                        cmd.CommandText = "UPDATE tbl_Product SET Qty = @Qty,TotalPrice = @TotalPrice  Where ID = " & key
                        cmd.Parameters.AddWithValue("@Qty", dic(key)(0) + total_Cart_Qty)
                        cmd.Parameters.AddWithValue("@TotalPrice", dic(key)(1) + total_price)
                        cmd.ExecuteNonQuery()
                        cmd.Parameters.Clear()
                    Next
                End Using
            End If
    
            MessageBox.Show("Update successfully!")

    希望对你有帮助。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    • 已标记为答案 christing 2021年3月8日 2:21
    2021年3月5日 7:29
    版主

全部回复

  • Hi christing,

    为了更好的分析和测试,有两个问题要确认一下。

    代码中 checkbox check的 data 指的是那部分内容?

    能否提供更多关于 ‘Product’ 的信息或代码?这将方便我们进行测试。

    期待你的更新。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    2021年2月25日 1:54
    版主
  • @xing yu zhao

    代码中 checkbox check的 data 指的是那部分内容?

    checkbox check的 data 是tbl_Cart 的data

     我的tbl_Product 是 vendor add store 的database 

    以下是Product 的代码

     Private Sub btnAddCart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCart.Click

            If txtProductQuantity1.Text = Nothing Then
                MessageBox.Show("OUT OF STOCK", "Information")
                Exit Sub
            Else
                Shopping_Cart.txtSID.Text = Me.txtID.Text
                Shopping_Cart.txtUserProductID.Text = Me.txtProductID1.Text
                Shopping_Cart.txtUserProductName.Text = Me.txtProdutName1.Text
                Shopping_Cart.txtUserProductPrice.Text = Me.txtProductPrice1.Text
                Vendor_Cart()
            End If

        End Sub

      Private Sub Vendor_Cart()

            'Product Part'
            Dim ProductID As String = txtProductID1.Text 'product ID'
            Dim ProductName As String = txtProdutName1.Text 'product name'
            Dim ProductQTY As String = txtProductQuantity1.Text 'product qty'
            Dim ProductPrice As String = txtProductPrice1.Text 'product single price'
            Dim ProductTotalPrice As Double = CDbl(txtProductTotalPrice1.Text)
            'Total'
            Dim totalProduct As String = Shopping_Cart.txtUserTotalPrice.Text
            Dim deduct As Integer = 1

            My_Cart()

            Try
                If txtProductQuantity1.Text = "0" Then
                    MessageBox.Show("OUT OF STOCK")
                    Exit Sub
                End If
                For A = 0 To CInt(ProductQTY) 'total qty'
                Next A

                txtProductQuantity1.Text = CStr(CDbl(ProductQTY) - deduct)
                txtProductTotalPrice1.Text = CStr(CDbl(ProductTotalPrice) - CDbl(ProductPrice))

            Catch ex As Exception

            End Try

        End Sub

       Private Sub My_Cart()


            'Product'
            Dim ProductQTY As String = txtProductQuantity1.Text 'product qty'
            Dim ProductPrice As String = txtProductPrice1.Text 'product single price'

            'Count Part'
            Dim ADD_T_QTY As String = Shopping_Cart.txtUserQuantity.Text
            Dim ADD_T_Price As String = Shopping_Cart.txtUserTotalPrice.Text

            ADD_T_QTY = CStr(Int(0))
            ADD_T_Price = CStr(Int(0))

            Dim Sum As Double

            Try
                If txtProductQuantity1.Text = "0" Then
                    Exit Sub
                Else
                    Counter = Counter + 1
                    Sum = CDbl(ADD_T_QTY)

                    For i = 1 To Counter

                        Shopping_Cart.txtUserQuantity.Text = CStr(Counter + Sum)
                        Shopping_Cart.txtUserTotalPrice.Text = CStr(CDbl(ProductPrice) * Counter)
                    Next i

                End If

            Catch ex As Exception

            End Try

        End Sub

    我的program 步骤是 vendor add store 进 tbl_Product 的datatable, user 可以在product page(属于tbl_Product) add 要的 stock 并且save 进 tbl_Cart 。

    感恩


    2021年2月25日 7:38
  • Hi christing,

    由于该问题涉及到的变量以及代码很多,为了更好的重现问题,你能将 project 分享到 onedrive 或者 github上面吗?

    我们会直接根据你提供的内容进行测试。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    2021年2月25日 8:21
    版主
  • @xing yu zhao 感谢你的回复

    你能将 project 分享到 onedrive 或者 github上面吗?

    >>可以的

    https://github.com/christingchin/shopping_cart/blob/main/Shopping_Cart.zip




    2021年2月26日 1:00
  • Hi @christing

    如果你想要将 datagridview 中 checkbox 选中的行更新到数据库的话,经过测试,你可以参考如下代码:

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim chk As DataGridViewCheckBoxCell = CType(row.Cells(0), DataGridViewCheckBoxCell)
                If chk.Value = True Then
    
                    Using con As New OleDbConnection
                        Dim cmd As New OleDbCommand
                        con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
                        con.Open()
                        cmd.Connection = con
                        cmd.CommandText = "UPDATE tbl_Cart SET ProductID = @ProductID, ProductName = @ProductName, CustomerName = @CustomerName, Cart_Qty = @Cart_Qty, Price = @Price, Total_Price = @Total_Price Where ID = " & Convert.ToInt32(row.Cells("ID").Value)
                        cmd.Parameters.AddWithValue("@ProductID", row.Cells("ProductID").Value.ToString)
                        cmd.Parameters.AddWithValue("@ProductName", row.Cells("ProductName").Value.ToString)
                        cmd.Parameters.AddWithValue("@CustomerName", row.Cells("CustomerName").Value.ToString)
                        cmd.Parameters.AddWithValue("@Cart_Qty", row.Cells("Cart_Qty").Value.ToString)
                        cmd.Parameters.AddWithValue("@Price", row.Cells("Price").Value.ToString)
                        cmd.Parameters.AddWithValue("@Total_Price", row.Cells("Total_Price").Value.ToString)
    
                        cmd.ExecuteNonQuery()
                    End Using
                End If
            Next
            MessageBox.Show("Update successfully!")
        End Sub

    希望对你有帮助。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).


    2021年2月26日 8:42
    版主
  • @HI XING YU ZHAO

    感谢你的答复但是我发现我update后的data 无法将tblProduct  quantity 改便。

    比如 tblProduct 有 10 QTY

    user 从 tblProduct 将product A 3 QTY 加进 tblCart , 之后user 又从 tblProduct 将product A 2 QTY 加进 tblCart 最后 tblProduct 只剩下5 QTY

    tblCart 就会变成有 5 QTY PRODUCT A 

    当 user checkout 所有 PRODUCT A return 回去tblProduct 时,tblProduct 的QTY 会变成 10 QTY

    当前代码的status 是 当user checkout 所有 PRODUCT A return 回去tblProduct 时

    PRODUCT A 会变成QTY 2 而并非10 .

    不知有什么方法可以让tblcart 的 PRODUCT A 和 tblProduct 的PRODUCT A  的 总数 加在一起变成 10 QTY 的总数

    谢谢指点。

    以下是我的代码

      For Each row As DataGridViewRow In DataGridView1.Rows
                Dim chk As DataGridViewCheckBoxCell = CType(row.Cells(0), DataGridViewCheckBoxCell)
                If chk.Value = True Then

                    Using con As New OleDbConnection
                        Dim cmd As New OleDbCommand
                        con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
                        con.Open()
                        cmd.Connection = con
                        cmd.CommandText = "UPDATE tbl_Product SET ProductID = @ProductID, ProductName = @ProductName, Qty = @Cart_Qty, Price = @Price, TotalPrice = @Total_Price Where ID = " & Convert.ToInt32(row.Cells("SID").Value)
                        cmd.Parameters.AddWithValue("@ProductID", row.Cells("ProductID").Value.ToString)
                        cmd.Parameters.AddWithValue("@ProductName", row.Cells("ProductName").Value.ToString)
                        cmd.Parameters.AddWithValue("@Qty", row.Cells("Cart_Qty").Value.ToString)
                        cmd.Parameters.AddWithValue("@Price", row.Cells("Price").Value.ToString)
                        cmd.Parameters.AddWithValue("@TotalPrice", row.Cells("Total_Price").Value.ToString)
                        cmd.ExecuteNonQuery()

                    End Using
                End If
            Next
            MessageBox.Show("Update successfully!")

    2021年3月2日 2:10
  • Hi christing,

    感谢你的回复。

    之前的回复似乎对你的问题存在误解,你能否分享一个 GIF 来描述你所作的工作,这样我们可以在该项目上重现你的问题。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    2021年3月2日 6:38
    版主
  • Hi xing yu zhao

    感谢你的回复以下是我的program 当前的状态

    不知怎的upload不了gif file 我将file upload 进 github 以下是我的link

    https://github.com/christingchin/shopping_cart




    2021年3月3日 0:51
  • Hi christing,

    感谢你的分享。

    > 当 user checkout 所有 PRODUCT A return 回去tblProduct 时 

    有个问题要再确认一下,用户 checkout是否意味着不会去 datagridview中 更改 PRODUCT A 对应的值,只是检查并决定要把哪个 PRODUCT A 再返回给 tblProduct 中, 也就是说,整个过程只有 'QTY' 和 'Cart_Qty' 的值会发生改变。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    2021年3月3日 6:37
    版主
  • Hi xing yu zhao, 

    感谢你的答复

    用户 checkout是否意味着不会去 datagridview中 更改 PRODUCT A 对应的值

    >>是的(除非user 正式对PRODUCT A 进行付款,就会从tblProduct 中扣除

    整个过程只有 'QTY' 和 'Cart_Qty' 的值会发生改变。

    >>是的 还有 Total_Price

    感谢你抽空的答复

    以下是我放在github 的program,请查看 谢谢

    https://github.com/christingchin/shopping_cart

    2021年3月3日 7:52
  • Hi christing,

    看看以下代码是否对你有帮助。

            Dim dic As Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer)
            Dim total_Cart_Qty As Integer = 0
            Dim id As Integer
            Dim isChecked As Boolean = False
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim chk As DataGridViewCheckBoxCell = CType(row.Cells(0), DataGridViewCheckBoxCell)
                If chk.Value = True Then
                    isChecked = True
                    id = Convert.ToInt32(row.Cells("SID").Value)
                    total_Cart_Qty = Convert.ToInt32(row.Cells("Cart_Qty").Value)
                    If Not dic.Keys.Contains(id) Then
                        dic.Add(id, total_Cart_Qty)
                    Else
                        dic(id) = dic(id) + total_Cart_Qty
                    End If
                End If
    
            Next
            If isChecked Then
                Using con As New OleDbConnection
                    con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
                    con.Open()
                    Dim cmd As New OleDbCommand
                    cmd.Connection = con
                    For Each key In dic.Keys
    
                        cmd.CommandText = "SELECT Qty FROM tbl_Product Where ID = " & key
                        Dim val = Convert.ToInt32(cmd.ExecuteScalar)
    
                        cmd.CommandText = "UPDATE tbl_Product SET Qty = @Qty Where ID = " & key
                        cmd.Parameters.AddWithValue("@Qty", dic(key) + val)
    
                        cmd.ExecuteNonQuery()
                        cmd.Parameters.Clear()
    
                    Next
                End Using
            End If
    
            MessageBox.Show("Update successfully!")

    上面代码是通过 checked 的值,更新 Product 表单中对应的 QTY值。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    • 已标记为答案 christing 2021年3月4日 6:05
    • 取消答案标记 christing 2021年3月4日 6:36
    2021年3月4日 3:21
    版主
  • @xing yu zhao 

    我想问如果要加total price 的值的话可以怎么做。

    我尝试加了以下代码但不成功,请查看  

    Dim total_Price As Integer = 0

    有关total_Price不知可以这么写?

       If Not dic.Keys.Contains(id) Then
                        dic.Add(id, total_Cart_Qty)

                         dic.Add(id, total_Cart_Qty + total_Price) x 反措值

                    Else
                        dic(id) = dic(id) + total_Cart_Qty + total_Price
                    End If
                End If

      cmd.CommandText = "UPDATE tbl_Product SET Qty = @Qty, TotalPrice = @TotalPrice Where ID = " & key
                        cmd.Parameters.AddWithValue("@Qty", dic(key) + val)
                        cmd.Parameters.AddWithValue("@TotalPrice", dic(key) + val)

    请指教。感恩指点

    2021年3月4日 6:05
  • Hi christing,

    如果也要存储 TotalPrice的话,这种情况就是一个 key(id) 对应两个 Value(Qty/TotalPrice), 可以考虑用 Dictionary(Of IKey, List(Of IValue)) 来存储。

    例如:

    Dim dic As Dictionary(Of Integer, List(Of Integer))
    dic(id) = lst

    你只需要定义一个 list, 然后在 lst 中存储 Qty 和 TotalPrice的值即可。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    2021年3月4日 8:50
    版主
  • @xing yu zhao

    我尝试了以下代码, 在 dic(id).Add(x) 会出现error Object reference not set to an instance of an object.


                    Dim items = New List(Of KeyValuePair(Of Integer, String))()
                    items.Add(New KeyValuePair(Of Integer, String)(1, total_Cart_Qty))
                    items.Add(New KeyValuePair(Of Integer, String)(1, total_Price))
                    Dim lookup = items.ToLookup(Function(kvp) kvp.Key, Function(kvp) kvp.Value)
                    For Each x As String In lookup(1)
                        dic(id).Add(x)

                    Next

    以下是我的完整代码


            Dim dic As Dictionary(Of Integer, List(Of Integer))
            Dim total_Price As Integer = 0
            Dim id As Integer
            Dim isChecked As Boolean = False
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim chk As DataGridViewCheckBoxCell = CType(row.Cells(0), DataGridViewCheckBoxCell)
                If chk.Value = True Then
                    isChecked = True
                    id = Convert.ToInt32(row.Cells("SID").Value)
                    Dim total_Cart_Qty As Integer
                    total_Cart_Qty = Convert.ToInt32(row.Cells("Cart_Qty").Value)
                    total_Price = Convert.ToInt32(row.Cells("Total_Price").Value)

                    Dim items = New List(Of KeyValuePair(Of Integer, String))()
                    items.Add(New KeyValuePair(Of Integer, String)(1, total_Cart_Qty))
                    items.Add(New KeyValuePair(Of Integer, String)(1, total_Price))
                    Dim lookup = items.ToLookup(Function(kvp) kvp.Key, Function(kvp) kvp.Value)
                    For Each x As String In lookup(1)
                        MessageBox.Show(x)
                        dic(id).Add(x)

                    Next

                End If
            Next

            If isChecked Then
                Using con As New OleDbConnection
                    con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
                    con.Open()
                    Dim cmd As New OleDbCommand
                    cmd.Connection = con
                    For Each key In dic.Keys

                        cmd.CommandText = "SELECT Qty FROM tbl_Product Where ID = " & key
                        Dim val = Convert.ToInt32(cmd.ExecuteScalar)

                        cmd.CommandText = "UPDATE tbl_Product SET Qty = @Qty, TotalPrice = @TotalPrice Where ID = " & key
                        'cmd.Parameters.AddWithValue("@Qty", dic(key) + val)
                        'cmd.Parameters.AddWithValue("@TotalPrice", dic(key) + val)
                        cmd.Parameters.AddWithValue("Qty", dic(key))
                        cmd.Parameters.AddWithValue("@TotalPrice", dic(key))
                        cmd.ExecuteNonQuery()
                        cmd.Parameters.Clear()

                    Next
                End Using
            End If

            MessageBox.Show("Update successfully!")
        End Sub


    2021年3月5日 1:07
  • Hi christing,

    尝试以下代码:

            Dim dic As Dictionary(Of Integer, List(Of Integer)) = New Dictionary(Of Integer, List(Of Integer))
            Dim total_Cart_Qty As Integer = 0
            Dim total_price As Integer
            Dim id As Integer
            Dim isChecked As Boolean = False
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim chk As DataGridViewCheckBoxCell = CType(row.Cells(0), DataGridViewCheckBoxCell)
                If chk.Value = True Then
                    isChecked = True
                    id = Convert.ToInt32(row.Cells("SID").Value)
                    total_Cart_Qty = Convert.ToInt32(row.Cells("Cart_Qty").Value)
                    total_price = Convert.ToInt32(row.Cells("Total_Price").Value)
    
                    If Not dic.Keys.Contains(id) Then
                        dic.Add(id, New List(Of Integer))
                        dic(id).Add(total_Cart_Qty)
                        dic(id).Add(total_price)
                    Else
                        dic(id)(0) = dic(id)(0) + total_Cart_Qty
                        dic(id)(1) = dic(id)(1) + total_price
                    End If
                End If
            Next
            If isChecked Then
                Using con As New OleDbConnection
                    con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;"
                    con.Open()
                    Dim cmd As New OleDbCommand
                    cmd.Connection = con
                    For Each key In dic.Keys
    
                        cmd.CommandText = "SELECT Qty,TotalPrice FROM tbl_Product Where ID = " & key
                        Dim reader = cmd.ExecuteReader
                        While reader.Read
                            total_Cart_Qty = Convert.ToInt32(reader("Qty"))
                            total_price = Convert.ToInt32(reader("TotalPrice"))
                        End While
                        reader.Close()
    
                        cmd.CommandText = "UPDATE tbl_Product SET Qty = @Qty,TotalPrice = @TotalPrice  Where ID = " & key
                        cmd.Parameters.AddWithValue("@Qty", dic(key)(0) + total_Cart_Qty)
                        cmd.Parameters.AddWithValue("@TotalPrice", dic(key)(1) + total_price)
                        cmd.ExecuteNonQuery()
                        cmd.Parameters.Clear()
                    Next
                End Using
            End If
    
            MessageBox.Show("Update successfully!")

    希望对你有帮助。

    Best Regards,

    Xingyu Zhao


    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    • 已标记为答案 christing 2021年3月8日 2:21
    2021年3月5日 7:29
    版主