vb.net - 如何从子表单刷新父表单上的数据网格
问题描述
在 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
```
解决方案
如果您想反映更改,即使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
推荐阅读
- vba - 选择多个 MS Word 页面并复制所选内容
- python - PyQt5 - 动态创建的小部件不显示 - 没有布局
- regex - Google RE2 Regex 转义句点和下划线错误
- nestjs - Nestjs - 控制器中定义的路由相互覆盖
- hive - 大表的 Hive 分区
- excel - 使用vba从路径导入图片
- docker - 更新后 Docker 守护进程无法启动
- bash - 在bash中将输出从日期文本更改为可用格式
- sql - MS Access:将带有组合框的过滤拆分表单转换为报告
- firebase - StreamBuilder 中的 Where 子句 Google Cloud Firestore