【问题标题】:UltraWinGrid refresh on separate form button pressUltraWinGrid 在单独的表单按钮按下时刷新
【发布时间】:2016-11-15 22:37:29
【问题描述】:

我有一个包含 UltraWinGrid 的 .NET 项目,用于显示数据库表中的数据。在 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

【问题讨论】:

    标签: vb.net refresh infragistics ultrawingrid


    【解决方案1】:

    您应该在调用表单中调用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
    

    【讨论】:

    • 嗨,史蒂夫,感谢您的回复,由于我不会使用或理解 c#,这段代码对于 vb.net 会有什么变化?
    猜你喜欢
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多