【问题标题】:Updating datagridview from another form in vb.net从 vb.net 中的另一种形式更新 datagridview
【发布时间】:2012-11-15 12:24:21
【问题描述】:

我有一个特定形式的数据网格视图。在该表单中有一个添加和更新按钮,它将接受详细信息,当单击完成按钮时,我想更新网格视图。但似乎我无法做到这一点,因为我正在从另一个表单添加/更新详细信息。

第一个表单(绑定数据,进入更新/添加按钮表单):

Private Sub bindStudentsData()
    Dim db As New Database()

    DataGridView1.DataSource = Nothing
    DataGridView1.DataSource = New BindingSource(db.getStudentList(), Nothing)

End Sub


Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click
    Dim sf As New StudentForm()
    sf.Show()
End Sub

Private Sub updateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateBtn.Click
    Dim sid As String = DataGridView1.SelectedRows(0).Cells("studentid").Value

    Dim sf As New StudentForm(sid)
    sf.Show()
End Sub

*通过使用 .show(),前一个表单保持打开状态并打开另一个表单

然后输入详细信息后点击完成 第二种形式:

Private Sub doneBtm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles doneBtm.Click
    Dim db As New Database()
    If type Is "add" Then
        'go to add
        MsgBox(type)
    Else
        'go to update
        db.updateStudentDetails(sid, astudentnumberText.Text, afirstnameText.Text, 
                     _alastnameText.Text, amiddlenameText.Text, asectionText.Text)
        Me.Close()
    End If
End Sub

单击第二个表单上的完成按钮后,我需要在第一个表单中重新绑定 datagridview。我怎么能那样做?有任何想法吗?谢谢!

【问题讨论】:

标签: vb.net winforms


【解决方案1】:

最简单和更好的方法是简单地使用ShowDialog() 而不是Show()。它会阻止您当前的表单,直到您关闭新打开的表单。然后致电您的bindStudentsData()。没有更多变化

Private Sub updateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateBtn.Click
    Dim sid As String = DataGridView1.SelectedRows(0).Cells("studentid").Value    
    Dim sf As New StudentForm(sid)
    sf.ShowDialog()
    bindStudentsData()
End Sub

第二种方法(你想要的)。复杂,但如果您不愿意不惜一切代价使用 ShowDialog

公开您的bindStudentsData() 并公开当前表单,即下一个表单的Owner

Private Sub updateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateBtn.Click
    Dim sid As String = DataGridView1.SelectedRows(0).Cells("studentid").Value    
    Dim sf As New StudentForm(sid)
    sf.Owner = Me
    sf.Show()
    bindStudentsData();
End Sub

现在您可以使用Owner 访问第二种形式的第一种形式,因此您也可以调用第一种形式的方法。只需在关闭第二个表单之前致电bindStudentsData()

Private Sub doneBtm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles doneBtm.Click
    Dim db As New Database()
    If type Is "add" Then
        'go to add
        MsgBox(type)
    Else
        'go to update
         db.updateStudentDetails(sid, astudentnumberText.Text, afirstnameText.Text, 
                 _alastnameText.Text, amiddlenameText.Text, asectionText.Text)
         Dim f1 As Form1 = TryCast(Me.Owner, Form1);
         // I have supposed that your first Form is Form1
         f1.bindStudentsData()
         Me.Close()
    End If
End Sub

【讨论】:

  • 感谢您的两个选择,我使用第二个建议,但方式不同。它仍然给了我将表单的现有实例传递给下一个表单的想法,这样我就可以访问第二个表单的方法。谢谢!
【解决方案2】:

完成后,您可以将数据保存在 db 中,然后回调您要刷新的表单。并且绑定将使用其中的更新/新数据刷新...

根据@kush 的评论,如果你想在工作完成后关闭第二个表单,你可以在响应中添加一个脚本标签来关闭页面。

【讨论】:

    【解决方案3】:

    我知道问题发布已经有一段时间了,但我使用了另一种解决方案,有些人可能会觉得它很有用。

    在 form1 中,我打开我的第二个表单:

    Dim frm As New Form1()
    frm.ShowDialog()
    

    然后,在第二种形式中,当我按下 OK 按钮时,我会写:

    DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
    

    最后,在form1中,我在frm.ShowDialog()下面添加了这些行:

    If frm.DialogResult = Windows.Forms.DialogResult.OK Then
        reaload_DTGV()
    End If
    

    使用此方法,您可以将 datagridview 的方法从 form1 保留为 Private

    【讨论】:

      【解决方案4】:

      这是我使用数据绑定源的方法:

      在我的 form1 按钮编辑中:

      edit_product.Show()
      edit_product.Item_numTextBox.Text = Item_numTextBox.Text
      Me.Close()
      

      然后在我的form2中:

      Private Sub Item_numTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Item_numTextBox.TextChanged
            InventoryBindingSource.Filter = "[item_num] like '%' + '" & Item_numTextBox.Text & "' + '%'"
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2019-05-30
        • 1970-01-01
        • 2021-08-06
        • 1970-01-01
        • 1970-01-01
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多