David David - 3 months ago 15
Vb.net Question

UltraWinGrid Auto Refresh

In my vb.net project, I have 3 forms.


has an UltraWinGrid on it, which is displaying some data. When I click the
add data
opens. When data is saved, the form then closes.
At this point, I want the UltraWinGrid on
to update/refresh and display the data that I added. At the moment, it doesn't display it until I close
and then open it again.

The images show this. (Data is not there at the start, it then gets added and does not appear. The final image is the form displaying the new data, after I've re-opened it.

enter image description here

How do I change this?

Current code:

To open the Add form

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

Using fp = New frmAddData(Globals.m_database)

If fp.ShowDialog() = DialogResult.OK Then

ugData.DataSource = Nothing

End If
End Using

End Sub

To save the entered information (on the Add form)

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

Dim m_cn As New OleDbConnection
m_cn = Globals.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")


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 = ""
Globals.savedValue = True


End If

End Sub

Call to load the database on the View form:

Public Sub getPeople()

Dim sql As String = "SELECT * FROM tblPerson ORDER BY [personID] ASC;"
Dim cm As New OleDbCommand(sql, Globals.m_database.getConnection())
Dim da As New OleDbDataAdapter(cm)
Dim dt As New DataTable()
ugData.DataSource = dt

Catch Ex As Exception
mdi1.errorLog(Ex.Message, Ex.StackTrace)
MsgBox("Failed to retrieve data, refer to error log")
End Try

End Sub


In a WinForm app a modal dialog is closed automatically by the engine if a button is pressed when its DialogResult property is set to anything but None. Then the Winform engine exits from the ShowDialog call and returns the DialogResult property of the button clicked. Usually this is more than enough to handle a situation like yours above.

In your code (as explained in chat) you have the DialogResult property of the Save button set to DialogResult.None. This, means that the Winforms engine doesn't close automatically your form and you need to write your own closing routine. But if you forget to set the Form property DialogResult to DialogResult.OK your calling code will never be able to refresh the grid because the test for DialogResult.OK fails.

So, whatever closing code you have to close the fromAddData instance remember to set the form DialogResult property

 Me.DialogResult = DialogResult.OK 

or if something is gone wrong

 Me.DialogResult = DialogResult.Cancel