我有某种形式的数据网格视图。在该表单中有一个添加和更新按钮,它将接受详细信息,当单击完成按钮时,我想更新gridview。但似乎我无法实现这一点,因为我正在添加/更新其他表单中的详细信息。
First Form(绑定数据,转到更新/添加按钮表单):
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。我怎么能这样做?有任何想法吗?谢谢!
答案 0 :(得分: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
答案 1 :(得分:0)
完成后,您可以将数据保存在db中,然后回调要刷新的表单。并且将使用更新/新数据刷新绑定...
从@kush的评论中,如果您希望在完成工作后关闭第二个表单,可以在Response witch中关闭页面添加脚本标记。
答案 2 :(得分:0)
我知道自问题发布以来已经有一段时间了,但我使用了另一种解决方案,有些人可能觉得它很有用。
在form1中,我打开第二个表单:
Dim frm As New Form1()
frm.ShowDialog()
然后,在第二种形式中,当我按确定按钮时,我写道:
DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
最后,在form1中,我在frm.ShowDialog()
下添加了这些行:
If frm.DialogResult = Windows.Forms.DialogResult.OK Then
reaload_DTGV()
End If
使用此方法,您可以将datagridview的方法从form1保持为私有。
答案 3 :(得分:-1)
以下是我如何使用 databindingsource 做到的:
在我的 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