首页 > 解决方案 > 如何从子表单刷新父表单上的数据网格

问题描述

在 frmBrand s上有一个 dgBrands,它在 subLoadDgBrands 中获取它的数据。在 frm Brand上,您可以在触发 btnSave_Click 事件处理程序时向 dtBrands 添加一条记录。相同的事件处理程序也调用 frmBrand s .subLoadDgBrands 但是它不会刷新显示的 dg。当您在 frm Brand上完成保存过程时,我需要它来刷新 frmBrand 上的 dg 因为我只想在保存成功时刷新,并且对于不同的形式,我需要将整数传回给该子

这是相关代码

    ```VB
        Public Class frmBrands
        
           Friend Sub subLoadDgBrands()
        
                dtBrands = fnGetBrand(0) 'Go get the data for the DataGridView
                dgBrands.DataBindings.Clear()
                dgBrands.DataSource = Nothing
                dgBrands.Rows.Clear()
                dgBrands.Columns.Clear()
        
                If dtBrands IsNot Nothing Then
                    dgBrands.DataSource = dtBrands
                    dgBrands.Refresh()
                    subFormatDgBrands()
                End If
            End Sub
        End Class
```

此事件处理程序需要刷新打开此表单的 frmBrand 实例上的数据网

    ```VB
      Public Class frmBrand
        
           Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
             
                'intID is passed by parent form = intBrand_ID
                'intSupplier_ID is declared at the top of the form and set correctly by event handler
                Dim intResult As Integer = 0 
                Dim strMessage As String = ""
                Dim strBrandName As String = txtBrand.Text
        
                'string builder that checks if the important fields are filled in
                If strBrandName = "" Then 'if there is nothing written in str
                    If strMessage = "" Then 'and if the error message is empty
                        strMessage = "Please provide a brand name" 'then add the error message to the string
                    Else 'if the error message isn't empty '
                        strMessage &= "and brand name" 'then add str to the message
                    End If
                End If
        
                If intSupplier_ID < 1 Then 'if there is a 0 or negative ID selected, can also use cboSupplier.selectedValue
                    If strMessage = "" Then 'and if the error message is empty
                        strMessage = "Please provide a supplier" 'then add the error message to the string
                    Else 'if the error message isn't empty
                        strMessage &= "and supplier" 'then add "Title" to the message
                    End If
                End If
        
                'save string checker 
                If strMessage <> "" Then 'if there is something in the message
                    MsgBox(strMessage) 'display it
        
                Else 'if there's nothing in the message
                    intResult = fnSaveBrand(intID,
                                            intSupplier_ID,
                                            strBrandName) 'We can save
        
                    'Additional logic loop that gives some feedback if it isn't saved
                    If intResult < 1 Then 'feedback number if the save wasn't succesfull
                        MsgBox("ID fault") 'Will let the user know it didn't save
                    Else 'the save was succesfull
                        **frmBrands.subLoadDgBrands()** 'Needs to refresh the datagrid on the instance of frmBrand**s** that this form was opened through
                        Me.Close() 'then it closes the form
                    End If
                End If
            End Sub
        End Class
```

标签: vb.netdatagridrefreshparent-child

解决方案


如果您想反映更改,即使frmBrand仍处于打开状态,subLoadDgBrands请设为公开,然后在执行插入查询后,添加此

Dim _frmBrands As frmBrands = TryCast(Me.Owner, frmBrands)
_frmBrands.subLoadDgBrands()

如果您想在frmBrand关闭后立即反映更改,请替换Me.CloseMe.DialogResult = Windows.Forms.DialogResult.OK. 然后在你的代码中打开frmBrand使用

Using frmBrand As New frmBrand()
      frmBrand.ShowDialog(Me)
          
      If frmBrand.DialogResult = Windows.Forms.DialogResult.OK Then
         subLoadDgBrands()
      End If

End Using

推荐阅读