如何使用VB.NET重置BindingSource/BindingList序列号更新到数据库

问题描述 投票:0回答:1

我正在尝试使用 VB.NET 重置 BindingSource/BindingList 序列号更新到数据库

或者我的代码实现中出了问题。

实际上这是指这篇文章的链接链接!但不同的是这篇文章会更新到数据库。

请指导我

谢谢

Public Class Formwithdb
    Private bindingSource As BindingSource = Nothing
    Dim productservice As New PurchaseService()
    Private Function GetProduct() As Product
        Return If(DataGridView1.SelectedCells.Count = 0, Nothing, TryCast(DataGridView1.SelectedCells(0).OwningRow.DataBoundItem, Product))
    End Function
    Private Sub Formwithdb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of Product)(CType(productservice.Getpurchase(), IList(Of Product)))}
        DataGridView1.DataSource = bindingSource
        DataGridView1.Columns("No").ReadOnly = True
        DataGridView1.Columns("Codeproduct").ReadOnly = True
        DataGridView1.Columns("Qty").ReadOnly = True
        DataGridView1.Columns("Qty_Stock").ReadOnly = True
        DataGridView1.Columns("Coldel").ReadOnly = True
    End Sub
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim dgv = DirectCast(sender, DataGridView)
        If e.RowIndex >= 0 AndAlso
        dgv.Columns("ColDel") Is dgv.Columns(e.ColumnIndex) Then
            Dim item = TryCast(dgv.Rows(e.RowIndex).DataBoundItem, Product)
            If item IsNot Nothing AndAlso
                MessageBox.Show("Confirm ...", "App", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Try
                    Dim i = 1
                    Dim bs = TryCast(bindingSource.DataSource, BindingList(Of Product))
                    Dim codeproduct = bs.FirstOrDefault(Function(x) x.Codeproduct = GetProduct.Codeproduct)
                    productservice.Deleteproduct(codeproduct)
                    bs.Remove(item)
                    For Each row As Product In bs
                        row.No = i
                        i += 1

'in the line of code below that I implemented incorrectly

                        Dim codeproductdetail = bs.FirstOrDefault(Function(x) row.Codeproduct = GetProduct.Codeproduct)
                        productservice.Updateproductno(codeproductdetail)
                    Next
                    DataGridView1.Refresh()
                    MessageBox.Show("Successfully")
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "App", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End Try
            End If
        End If
    End Sub
End Class
Public Class PurchaseService
   Public Sub Updateproductno(ByVal Obj As Product)
        Dim sql = $"UPDATE Product Set [No] = {Obj.No} WHERE Codeproduct ='{Obj.Codeproduct}';"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            _conn.Execute(sql)
        End Using
    End Sub
End Class

根据 @dr.null 的指南和下面的代码问题已解决

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim dgv = DirectCast(sender, DataGridView)
        If e.RowIndex >= 0 AndAlso
        dgv.Columns("ColDel") Is dgv.Columns(e.ColumnIndex) Then
            Dim item = TryCast(dgv.Rows(e.RowIndex).DataBoundItem, Product)
            If item IsNot Nothing AndAlso
                MessageBox.Show("Confirm ...", "App", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Try
                    Dim i = 1
                    Dim bs = TryCast(bindingSource.DataSource, BindingList(Of Product))
                    Dim codeproduct = bs.FirstOrDefault(Function(x) x.Codeproduct = GetProduct.Codeproduct)
                    productservice.Deleteproduct(codeproduct)
                    bs.Remove(item)
                    For Each row As Product In bs
                        row.No = i
                        i += 1
                        Dim Update = New Product With {
                                 .No = row.No,
                                 .Codeproduct = row.Codeproduct}
                        productservice.Updateproductno(Update)
                    Next
                    DataGridView1.Refresh()
                    MessageBox.Show("Successfully")
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "App", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End Try
            End If
        End If
    End Sub
Public Class PurchaseService
   Public Sub Updateproductno(ByVal Obj As Product)
        Dim sql = $"UPDATE Product Set [No] = {Obj.No} WHERE Codeproduct ='{Obj.Codeproduct}';"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            _conn.Execute(sql)
        End Using
    End Sub
End Class
vb.net datagridview dapper bindingsource bindinglist
1个回答
0
投票

这要归功于@dr.null 的帮助和建议。我非常感谢你

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim dgv = DirectCast(sender, DataGridView)
        If e.RowIndex >= 0 AndAlso
        dgv.Columns("ColDel") Is dgv.Columns(e.ColumnIndex) Then
            Dim item = TryCast(dgv.Rows(e.RowIndex).DataBoundItem, Product)
            If item IsNot Nothing AndAlso
                MessageBox.Show("Confirm ...", "App", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Try
                    Dim i = 1
                    Dim bs = TryCast(bindingSource.DataSource, BindingList(Of Product))
                    Dim codeproduct = bs.FirstOrDefault(Function(x) x.Codeproduct = GetProduct.Codeproduct)
                    productservice.Deleteproduct(codeproduct)
                    bs.Remove(item)
                    For Each row As Product In bs
                        row.No = i
                        i += 1
                        Dim Update = New Product With {
                                 .No = row.No,
                                 .Codeproduct = row.Codeproduct}
                        productservice.Updateproductno(Update)
                    Next
                    DataGridView1.Refresh()
                    MessageBox.Show("Successfully")
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "App", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End Try
            End If
        End If
    End Sub
Public Class PurchaseService
   Public Sub Updateproductno(ByVal Obj As Product)
        Dim sql = $"UPDATE Product Set [No] = {Obj.No} WHERE Codeproduct ='{Obj.Codeproduct}';"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            _conn.Execute(sql)
        End Using
    End Sub
End Class

© www.soinside.com 2019 - 2024. All rights reserved.