使用 VB.NET 将数据网格视图中的选定行显示到 TextBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36518037/
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
Display selected row from data grid view to TextBox using VB.NET
提问by Silver Archer
I was working with VB.NET and SQL Server database. I want to display selected row into the textbox so that I can edit them and update to database. But when I finished my code, it comes out an exception told me the column name is not found. I double checked the column name and confirm I was spelling correct. What is the problem? Here is my code:
我正在使用 VB.NET 和 SQL Server 数据库。我想在文本框中显示选定的行,以便我可以编辑它们并更新到数据库。但是当我完成我的代码时,它出现一个异常告诉我没有找到列名。我仔细检查了列名并确认我拼写正确。问题是什么?这是我的代码:
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
TextBox1.Text = row.Cells("StudentID").Value.ToString
TextBox2.Text = row.Cells("StudentName").Value.ToString
TextBox3.Text = row.Cells("HomeAddress").Value.ToString
TextBox4.Text = row.Cells("ContactNumber").Value.ToString
TextBox5.Text = row.Cells("SubjectCode").Value.ToString
TextBox6.Text = row.Cells("SubjectName").Value.ToString
TextBox7.Text = row.Cells("ParentName").Value.ToString
TextBox8.Text = row.Cells("ParentContact").Value.ToString
End If
End Sub
Here is my database:
这是我的数据库:
Also I want to ask how can I update/refresh my database by using a button? I look through online but nothing effective, I want to refresh the database after I add/edit/delete a data.
另外我想问一下如何使用按钮更新/刷新我的数据库?我在网上浏览但没有任何效果,我想在添加/编辑/删除数据后刷新数据库。
Thank you very much.
非常感谢。
采纳答案by jmcilhinney
You're going about this the wrong way. I'm guessing that you are populating a DataTablefrom the database. You should then be binding that DataTableto a BindingSourceand you should then be binding that to the DataGridViewANDthe TextBoxcontrols. The TextBoxcontrols will then automatically be populated when you select a grid row ANDany changes you make in the TextBoxcontrols will be automatically pushed back to the DataTableand the grid.
你这样做是错误的。我猜你是DataTable从数据库中填充的。那么你就应该绑定DataTable到一个BindingSource,然后你要绑定到DataGridView与该TextBox控件。该TextBox控件将在您选择网格行,然后自动填充和你在做任何修改TextBox控件将被自动推回DataTable和电网。
To bind a TextBox, select it in the designer and then open the Properties window. You can then expand the (DataBindings) node and select the Textproperty under that. From there you can select the BindingSourceas the data source and specify which column to bind to.
要绑定TextBox,请在设计器中选择它,然后打开“属性”窗口。然后您可以展开 (DataBindings) 节点并选择其Text下的属性。从那里您可以选择BindingSource为数据源并指定要绑定到的列。
回答by nash
Use this:
用这个:
Private Sub dgvEmployees_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEmployees.CellContentClick
textbox1.Text = datagridview.Rows(e.RowIndex).Cells(0).Value
end sub

