【发布时间】: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