简体   繁体   English

直到单击按钮调用DataGridView单元格值更改才会发生

[英]DataGridView Cell Value Change Not Happening untill i call from button click

I have retrieved Product List from access database into a DataGridView. 我已经从Access数据库中将产品列表检索到DataGridView中。 here is code: 这是代码:

Sub loadProductList()
    Try
        Using dbConn As New OleDb.OleDbConnection
            dbConn.ConnectionString = dbProvider & dbSource
            dbConn.Open()
            Dim thisSQL As String
            thisSQL = "SELECT productID, productName, productCostPrice From " & tblProduct & " WHERE productBrandID=" & CType(txtBrandName.Tag, Integer)
            Dim Dadapter As New OleDbDataAdapter(thisSQL, dbConn)
            Dim DSet As New DataSet
            Dadapter.Fill(DSet, tblProduct)
            With DataGridView1
                .Columns.Clear()
                .DataSource = DSet.Tables(tblProduct)
                With .Columns.Item(0)
                    .Width = 50
                    .HeaderText = "Code"
                    .Name = "Code"
                End With
                Dim txtColumn As New DataGridViewTextBoxColumn
                With txtColumn
                    .Width = 50
                    .HeaderText = "Qty"
                    .Name = "Qty"
                End With
                .Columns.Insert(1, txtColumn)
                With .Columns.Item(2)
                    .Width = 490
                    .HeaderText = "Description"
                End With
                With .Columns.Item(3)
                    .Width = 100
                    .HeaderText = "Price"
                    .Name = "Price"
                    .DefaultCellStyle.Format = "N"
                End With
                Dim txtaColumn As New DataGridViewTextBoxColumn
                .Columns.Insert(4, txtaColumn)
                With .Columns.Item(4)
                    .Name = "Amount"
                    .HeaderText = "Amount"
                    .Width = 120
                    .DefaultCellStyle.Format = "N"
                End With
            End With
        End Using
    Catch ex As Exception

    End Try
End Sub
Sub UpdatePurchaseItemList()
    Try
            Using dbConn As New OleDb.OleDbConnection
            dbConn.ConnectionString = dbProvider & dbSource
            dbConn.Open()
            Dim thisSQL As String
            thisSQL = "SELECT * FROM " & tblPurchaseItems & " WHERE PitemPID='" & txtRefID.Text & "'"
            Using dbDataCmd As New OleDbCommand(thisSQL, dbConn)
                Dim dbDataRead As OleDbDataReader
                dbDataRead = dbDataCmd.ExecuteReader()
                Do While dbDataRead.Read = True
                    For i As Integer = 0 To DataGridView1.Rows.Count - 1
                        With DataGridView1.Rows(i)
                            If .Cells("Code").Value = dbDataRead("PitemProductID") Then
                                .Cells("Qty").Value = dbDataRead("PitemProductQty")
                                .Cells("Price").Value = dbDataRead("PitemCost")
                                .Cells("Amount").Value = dbDataRead("PitemTotal")
                                Exit For
                            End If
                        End With
                    Next
                Loop
            End Using
        End Using
    Catch ex As Exception

    End Try
End Sub

on form load i am calling both sub: 在窗体加载时,我同时调用两个子:
loadProductList() loadProductList()
UpdatePurchaseItemList() ' this does not update any thing UpdatePurchaseItemList()'这不会更新任何东西

but when i call this sub on button click even it works perfectly. 但是当我在按钮上单击此子按钮时,它甚至可以正常工作。

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    UpdatePurchaseItemList() 'it shows the updates. 
End Sub

please tell me why it does not show updates? 请告诉我为什么它不显示更新? Thanks 谢谢

I'm not able to test right now, but try endedit() after you update your datagridview: 我目前无法测试,但是在更新datagridview之后尝试endedit():

                With DataGridView1.Rows(i)
                    If .Cells("Code").Value = dbDataRead("PitemProductID") Then
                        .Cells("Qty").Value = dbDataRead("PitemProductQty")
                        .Cells("Price").Value = dbDataRead("PitemCost")
                        .Cells("Amount").Value = dbDataRead("PitemTotal")
                        DataGridView1.EndEdit()
                        Exit For
                    End If
                End With

That should 'Commits and ends the edit operation on the current cell.' 那应该是“在当前单元格上提交并结束编辑操作”。 as per MSDN Datagridview.EndEdit Method 根据MSDN Datagridview.EndEdit方法

ok try this trick it will automatically update the data on you're datagridview 好的,尝试这个技巧,它将自动更新您的datagridview上的数据

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    RefreshData()
End Sub


 Private Sub RefreshData()

    Dim da As OleDbDataAdapter
    Dim ds As New DataSet


    If Not cnn.State = ConnectionState.Open Then
        'open connection
        cnn.Open()
    End If

    da = New OleDb.OleDbDataAdapter("Select * FROM tblPurchaseItems", cnn)


    'Dim dt As New DataSet
    'fill data to datatable
    da.Fill(ds)

    'offer data in data table into datagridview
    Me.DataGridView1.DataSource = ds.Tables(0)
    Me.DataGridView1.Refresh()
    'close connection
    cnn.Close()

End Sub

and then copy and paste RefreshData() before the end of your, add,edit,delete sub statement 然后在您的add,edit,delete子语句结束之前复制并粘贴RefreshData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM