问题
In my vb.net project, I have 3 forms. home_mdi
, Viewfrm
and AddDatafrm
.
Viewfrm
has an UltraWinGrid on it, which is displaying some data. When I click the add data
button, AddDatafrm
opens. When data is saved, the form then closes.
At this point, I want the UltraWinGrid on Viewfrm
to update/refresh and display the data that I added. At the moment, it doesn't display it until I close Viewfrm
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.
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
getPeople()
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")
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 = ""
Globals.savedValue = True
Me.Close()
End If
End Sub
Call to load the database on the View form:
Public Sub getPeople()
Try
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()
da.Fill(dt)
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
回答1:
In a WinForm app, a modal dialog is closed automatically by the engine if a button is pressed and its DialogResult
property is set to anything but None. Then the Winform engine sets the form DialogResult to the same property of the button, exits from the ShowDialog call and returns the DialogResult property of the button clicked.
Usually this is more than enough to handle situations like yours above. (Or, in general situations where a user choose between Yes/No or OK/Cancel scenarios)
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 with
Me.DialogResult = DialogResult.OK
or, if something is gone wrong, with
Me.DialogResult = DialogResult.Cancel
来源:https://stackoverflow.com/questions/38396728/ultrawingrid-auto-refresh