我有一个.NET项目,其中包含一个UltraWinGrid来显示数据库表中的数据。在UWG的表格上,我有3个按钮; “新数据”,“编辑数据”和“删除数据”。前两个用控件打开新表单,通过控件输入/编辑要保存的数据。保存功能正常,但是当我关闭表单以查看初始表单(使用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
答案 0 :(得分:2)
您应该在呼叫表单中调用getPeople
功能,而不是在保存表单中
您只需要知道保存表单是否已正确终止,并且此信息可用作对ShowDialog
的调用的返回值。保存人员数据的按钮应将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