2

製品リストをアクセス データベースから DataGridView に取得しました。コードは次のとおりです。

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

フォームの読み込み時に、両方のサブを呼び出しています:
loadProductList()
UpdatePurchaseItemList() ' これは何も更新しません

しかし、ボタンクリックでこのサブを呼び出すと、完全に機能します。

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

更新が表示されない理由を教えてください。ありがとう

4

2 に答える 2

0

今はテストできませんが、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

これは、「現在のセルの編集操作をコミットして終了する」必要があります。MSDN Datagridview.EndEdit メソッドに従って

于 2013-04-15T06:17:58.960 に答える