如何使用 vb.net 刷新或重新加载 datagrdiview 中的绑定源

How to refresh or reload binding source in datagrdiview with vb.net

提问人:siwa86 提问时间:9/17/2023 最后编辑:siwa86 更新时间:9/17/2023 访问量:52

问:

更新后,我试图调用“loaddata”,但datagridview中的结果不匹配,如果我关闭调试表单,然后重新调试表单,那么新的datagridview视图的结果是合适的。代码有问题吗?

谢谢

Private BindingSource1 As BindingSource = Nothing
Private ResultList As IEnumerable(Of ProductOut) = Nothing
Private Sub loaddata()
        Using conn = New OleDbConnection(connectionString)
            conn.Open()
            resultList = conn.Query(Of ProductOut)("SELECT p.ProductId, p.ProductName,p.Price,p.Qty,s.QtyStock FROM ProductOut p INNER JOIN StockProduct s ON s.ProductId = p.ProductId").ToList()
            conn.Close()
        End Using
        bindingSource1 = New BindingSource With {.DataSource = New BindingList(Of ProductOut)(CType(resultList, IList(Of ProductOut)))}
        DataGridView1.DataSource = bindingSource1
    End Sub
 Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles BtnUpdate.Click
        ' Change quantity here.
  Dim product = GetProduct()
  ElseIf product IsNot Nothing Then
                ' Update product's quantity.
                product.Qty += CInt(NumericUpDownQTY.Value)
                DataGridView1.Refresh()
  conn.Open()
                    Try
                        Dim count2 As Integer = conn.Execute("UPDATE StockProduct INNER JOIN ProductOut On (StockProduct.ProductId = ProductOut.ProductId) Set StockProduct.QtyStock = (StockProduct.QtyStock-(@Param1-ProductOut.Qty)), StockProduct.QtyOut = (StockProduct.QtyOut+(@Param2-ProductOut.Qty)) WHERE ProductOut.Productid = @PARAM3;",
                        New With {
                            Key .param1 = product.Qty,
                            Key .param2 = product.Qty,
                            Key .param3 = product.ProductId})
                    Dim count As Integer = conn.Execute("UPDATE ProductOut SET
                    Qty = @param1
                    WHERE ProductId = @param2",
                        New With {
                            Key .param1 = product.Qty,
                            Key .param2 = product.ProductId})
                   MessageBox.Show("successfully")
                    loaddata()
                    Catch ex As Exception
                        MessageBox.Show(ex.Message, "POS", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    End Try
            End If
        End Using
    End Sub

更新 Capture1 之前

before update Capture1

更新后捕获2

AFTER UPDATE Capture2

vb.net DataGridView BindingSource BindingList

评论

0赞 nbk 9/17/2023
这回答了你的问题吗?从数据网格视图更新数据库 vb.net 如您所见,您通过更新适配器来更新 DataGriedView 和传输 chages
0赞 siwa86 9/17/2023
@nbk ,感谢您的回复,您提到使用,但我使用 dapper 到 bindinglist,然后到 bindingsource,然后绑定到 datagridviewCommandBuilder and adapter
0赞 nbk 9/17/2023
好的,你没有写你的问题,但你已经更新了 DataGridView,所以你已经达到了 Dazr,不需要一个新的 bindibg
0赞 siwa86 9/18/2023
@nbk,谢谢你的回复,对不起,我回复晚了。 只有该列不是最新的,并且该列来自内部联接。所以那里有一个问题,所以我必须重新加载或再次刷新a new binding would not be necessaryQtyStock
0赞 nbk 9/18/2023
那是因为,你只更新一列,一次性为两个列做

答: 暂无答案