David David - 4 months ago 23
Vb.net Question

UltraWinGrid refresh on separate form button press

I have a .NET project that includes an UltraWinGrid to display data from a database table. On the form with the UWG, I have 3 buttons; 'New Data', 'Edit Data' and 'Delete Data'. The first two open new forms with controls through which to enter/edit the data to be saved. The save function works fine, however when I close the form to see the initial form (with the UWG), the data has not refreshed, and only does so when I close and re-open it.

So, is there any way that I can make the UWG refresh when I press the save button on the new form? (I have already tried calling the function again which loads the UWG, but this doesn't work as I cannot make it a Shared method due to the connections)

Save function code:

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


Code that loads the 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

Answer

You should call the getPeople function in the calling form, not in the saving form.
You just need to know if the saving form has terminated correctly or not and this information is available as the return value for the call to ShowDialog. Your button that saves the person data should have the DialogResult property set to OK and then this code should work for you

' 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

Notice the point if all goes well. This means that if, in your button save procedure something does not go well, you should block the form closing changing the DialogResult property to 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