UltraWinGrid自动刷新 [英] UltraWinGrid Auto Refresh

查看:214
本文介绍了UltraWinGrid自动刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的vb.net项目中,我有3种形式. home_mdiViewfrmAddDatafrm.

In my vb.net project, I have 3 forms. home_mdi, Viewfrm and AddDatafrm.

Viewfrm上有一个UltraWinGrid,它正在显示一些数据.当我单击add data按钮时,将打开AddDatafrm.保存数据后,表格将关闭. 此时,我希望Viewfrm上的UltraWinGrid更新/刷新并显示添加的数据.目前,直到我关闭Viewfrm然后再次打开它时,它才会显示.

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.

我该如何更改?

当前代码:

打开添加"表单

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

推荐答案

在WinForm应用程序中,如果按下按钮并将其DialogResult属性设置为None,则引擎会自动关闭模式对话框.然后,Winform引擎将窗体DialogResult设置为按钮的相同属性,退出ShowDialog调用并返回所单击按钮的DialogResult属性.
通常,这足以应付上述情况. (或者,在通常情况下,用户在是/否"或确定/取消"方案之间进行选择)

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)

在代码中(如聊天中所述),将保存"按钮的DialogResult属性设置为DialogResult.None.这意味着Winforms引擎不会自动关闭您的表单,您需要编写自己的关闭例程.

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.

但是,如果您忘记将Form属性DialogResult设置为DialogResult.OK,则由于DialogResult.OK的测试失败,因此调用代码将永远无法刷新网格.

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.

因此,无论您要关闭fromAddData实例的关闭代码如何,都请记住将DialogResult属性设置为

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

这篇关于UltraWinGrid自动刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆