UltraWinGrid 在单独的表单按钮按下时刷新

UltraWinGrid refresh on separate form button press

我有一个 .NET 项目,其中包含一个 UltraWinGrid 来显示来自数据库的数据 table。在 UWG 的表单上,我有 3 个按钮; 'New Data'、'Edit Data' 和 'Delete Data'。前两个打开带有控件的新表单,通过这些控件可以 enter/edit 要保存的数据。保存功能工作正常,但是当我关闭表单以查看初始表单(使用 UWG)时,数据没有刷新,只有在我关闭并重新打开它时才会刷新。

那么,有什么方法可以让我在新表单上按下保存按钮时刷新 UWG? (我已经尝试再次调用加载 UWG 的函数,但这不起作用,因为由于连接我无法将其设为共享方法)

保存功能代码:

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    Dim m_cn As New OleDbConnection
    m_cn = m_database.getConnection()


    If txtFirstName.Text = "" Then
        MsgBox("First name cannot be blank")

    ElseIf txtLastName.Text = "" Then
        MsgBox("Last name cannot be blank")

    ElseIf txtAge.Text = "" Then
        MsgBox("Age cannot be blank")

    ElseIf txtPostCode.Text = "" Then
        MsgBox("Postcode cannot be blank")

    Else
        Dim personID As Integer = database.SaveNewPerson(txtFirstName.Text, txtLastName.Text, txtAge.Text, txtPostCode.Text, m_cn)

        MsgBox("Save successful")

        txtFirstName.Text = ""
        txtLastName.Text = ""
        txtAge.Text = ""
        txtPostCode.Text = ""

    End If

End Sub

加载 UWG 的代码:

 Public Sub getPeople()

    Try
        Dim sql As String = "SELECT * FROM tblPerson"
        Dim cm As New OleDbCommand(sql, m_database.getConnection())
        Dim da As New OleDbDataAdapter(cm)
        Dim dt As New DataTable()
        da.Fill(dt)
        ugData.DataSource = dt

    Catch Ex As Exception
        MsgBox("Could not load people")
    End Try

End Sub

您应该在调用表单中调用 getPeople 函数,而不是在保存表单中。
您只需要知道保存表单是否已正确终止,并且此信息可作为调用 ShowDialog 的 return 值使用。您保存人员数据的按钮应将 DialogResult 属性 设置为 OK 然后此代码应该适合您

' Open the form that inserts a new person
' Change that name to your actual form class name...
Using fp = new frmPerson()

   ' If the user presses the button to save and everything goes well
   ' we get the DialogResult property from the button pressed 
   if fp.ShowDialog() = DialogResult.OK Then

       ugData.DataSource = Nothing
       getPeople()

   End If
End Using

注意要点如果一切顺利。这意味着如果在您的按钮保存过程中出现问题,您应该阻止关闭表单,将 DialogResult 属性 更改为 None.

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    Dim m_cn As New OleDbConnection
    m_cn = m_database.getConnection()


    If txtFirstName.Text = "" Then

        MsgBox("First name cannot be blank")
        Me.DialogResult = DialogResult.None
    ElseIf txtLastName.Text = "" Then
        MsgBox("Last name cannot be blank")
        Me.DialogResult = DialogResult.None
    ElseIf txtAge.Text = "" Then
        MsgBox("Age cannot be blank")
        Me.DialogResult = DialogResult.None
    ElseIf txtPostCode.Text = "" Then
        MsgBox("Postcode cannot be blank")
        Me.DialogResult = DialogResult.None
    Else
        Dim personID As Integer = database.SaveNewPerson(txtFirstName.Text, txtLastName.Text, txtAge.Text, txtPostCode.Text, m_cn)

        MsgBox("Save successful")

        txtFirstName.Text = ""
        txtLastName.Text = ""
        txtAge.Text = ""
        txtPostCode.Text = ""
    End If
End Sub