2016-07-13 16 views
0

刷新我有一個.NET項目,其包括UltraWinGrid從數據庫表顯示數據。在UWG的表格中,我有3個按鈕; '新數據','編輯數據'和'刪除數據'。前兩個打開新窗體,通過它控制輸入/編輯要保存的數據。保存功能工作正常,但是當我關閉形式來看初始的形式(使用UWG),數據沒有更新,只有這樣做,當我關閉並重新打開它。UltraWinGrid在單獨的表格按鈕按壓

那麼,有沒有什麼辦法,我可以使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 

回答

2

你應該調用getPeople函數調用的形式,而不是在儲蓄形式。
你只需要知道,如果儲蓄形式已經正確與否終止,該信息可作爲調用ShowDialog返回值。您的按鈕保存數據的人應該有DialogResult屬性設置爲確定,那麼這段代碼應該爲你工作

' 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屬性設置爲無。

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 
+1

嗨史蒂夫,感謝您的迴應,該代碼如何改變爲vb.net,因爲我不使用或理解c#? – David

+0

現在修復它... – Steve

+1

可愛,謝謝! – David