从 vb.net 中的另一种形式更新 datagridview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13397507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating datagridview from another form in vb.net
提问by Luke Villanueva
I have a datagridview at a certain form. In that form there is a add and update button which will accept details and when the finish button is click, I want to update the gridview. But it seems I cannot accomplish this since I'm adding/updating details from another form.
我有某种形式的数据网格视图。在该表单中有一个添加和更新按钮,它将接受详细信息,当单击完成按钮时,我想更新 gridview。但似乎我无法完成此操作,因为我正在从另一个表单添加/更新详细信息。
First Form (Bind data, go to update/add button 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
*By using .show() the previous form stays open and opens another form
*通过使用 .show() 前一个表单保持打开状态并打开另一个表单
Then after entering the details and click finish Second Form:
然后在输入详细信息并单击完成第二个表格后:
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
I need to rebind the datagridview in my first form after clicking done button on the second form. How could I do that? Any ideas? Thanks!
单击第二个表单上的完成按钮后,我需要在第一个表单中重新绑定 datagridview。我怎么能那样做?有任何想法吗?谢谢!
采纳答案by Sami
Simplest and better method is to simply use ShowDialog()instead of Show(). It blocks your current form until you close the newly opened form. Then Call your bindStudentsData(). No more changes
最简单和更好的方法是简单地使用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
Second Method(which you want). Complex but if you are not willing to use ShowDialog at any Cost
第二种方法(你想要的)。复杂但如果您不愿意不惜一切代价使用 ShowDialog
Make your bindStudentsData()public and make current form, the Ownerof next Form
让你的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
Now you can access first form in second form by using Ownerand hence you can call methods of first form as well. Just call bindStudentsData()before closing second form
现在您可以通过 using 访问第二种形式的第一种形式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
回答by nbadaud
I know it's been a while since the question was posted but I use another solution and some people may find it usefull.
我知道问题发布已经有一段时间了,但我使用了另一种解决方案,有些人可能会觉得它很有用。
In the form1, I open my second form :
在 form1 中,我打开第二个表单:
Dim frm As New Form1()
frm.ShowDialog()
Then, in the second form, when I press the OK buttonI write :
然后,在第二种形式中,当我按下OK 按钮时,我写道:
DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
Finally, in form1, I add these lines under frm.ShowDialog():
最后,在 form1 中,我在下面添加这些行frm.ShowDialog():
If frm.DialogResult = Windows.Forms.DialogResult.OK Then
reaload_DTGV()
End If
With this method, you can keep your datagridview's methods from form1 to Private.
使用此方法,您可以将 datagridview 的方法从 form1 保留到 Private。
回答by Minus
When you finish, you could save your data in db, then call back the form you want to refresh. And the binding will be refresh with the updated/new data in it...
完成后,您可以将数据保存在db中,然后回调您要刷新的表单。并且绑定将使用其中的更新/新数据刷新...
From the comment of @kush, if you wish to close the second form after work is done, you can add a script tag in Response witch close the page.
根据@kush 的评论,如果您希望在工作完成后关闭第二个表单,您可以在关闭页面的 Response 中添加一个脚本标签。

