【问题标题】:How to refresh a datagrid on a parent form, from a child form如何从子表单刷新父表单上的数据网格
【发布时间】:2021-06-15 20:05:59
【问题描述】:

在 frmBrands 上有一个 dgBrands,它在 subLoadDgBrands 中获取它的数据。在 frmBrand 上,您可以在触发 btnSave_Click 事件处理程序时将记录添加到 dtBrands。相同的事件处理程序也调用 frmBrands.subLoadDgBrands 但是它不会刷新显示的 dg。当您在 frmBrand 上完成保存过程时,我需要它来刷新 frmBrands 上的 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
```

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

    ```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
```

【问题讨论】:

  • 第三种形式,frmSupplier 有一个几乎相同的保存过程,之后我想将一个整数传回其父窗体 frmBrand 并刷新其 cboSupplier 并将 selectedValue 设置为传递的整数,以便它选择您刚刚添加的记录。像这样; VB public class frmSupplier Private Sub btnSave_Click (Shortened to make it brief) **frmBrand.subLoadCboSupplier(intResult) Me.Close() End Sub End Class 我无法让它工作,我不明白为什么。

标签: vb.net datagrid refresh parent-child


【解决方案1】:

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

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

如果您想在frmBrand 关闭后立即反映更改,请将Me.Close 替换为Me.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

【讨论】:

  • 是的,保存有效,我的意思是,如果我关闭并重新打开程序,我刚刚添加的数据就在那里。为什么我不只是在表单调用下运行刷新子是因为我只想在保存成功的情况下在其父表单上运行刷新子。我想对第三层使用相同的结构,该结构还允许我向 refreshsub 传递从保存返回的整数,因此它可以使用它来设置 combobox.selectedValue 给它。
  • 你考虑过使用DialogResult吗?如果子表单以 DialogResult = OK 关闭,则刷新您的 datagridview。
  • 这似乎与我的情况无关。我只想从 frmBrand 触发 frmBrandS.subLoadDgBrands,并可选择传递一个变量作为参数。我该怎么做?
  • 您的意思是您不想关闭frmBrand,但更改仍应反映在dgBrands 中?
  • 我已经更新了我的答案,检查它是否适合你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 2012-06-08
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多