vb.net 如何更新数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18490114/
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
How to Update DataTable
提问by Alex
I have a main DataSet
which contains several DataTables
. These different DataTables are bound to their DataGridView's DataSource respectively.
我有一个DataSet
包含多个DataTables
. 这些不同的DataTable 分别绑定到它们的DataGridView 的DataSource。
My problem is whenever I modify something in the display area such as the description textbox below and then click on save....
我的问题是每当我修改显示区域中的某些内容时,例如下面的描述文本框,然后单击保存....
<<< To >>>
<<< 至 >>>
The DataGridView doesn't have the replicated changes, as seen here:
DataGridView 没有复制的更改,如下所示:
I need a way to update the DataTable ... My save button successfully saves the information. I am quite new to DataSets and DataTables so this is my first time trying to update a DataTable. I doubt I need to reload the information in the DataTable, there must be something more efficient?
我需要一种方法来更新数据表......我的保存按钮成功保存了信息。我对数据集和数据表很陌生,所以这是我第一次尝试更新数据表。我怀疑我需要重新加载 DataTable 中的信息,一定有更有效的东西吗?
回答by Alex
Updating a DataTable With Unknown Indexes
使用未知索引更新数据表
For more information: How to: Edit Rows in a DataTable
有关更多信息:如何:编辑数据表中的行
In order to edit an existing row in a DataTable, you need to locate the DataRow you want to edit, and then assign the updated values to the desired columns.
为了编辑 DataTable 中的现有行,您需要找到要编辑的 DataRow,然后将更新的值分配给所需的列。
Update existing records in typed datasets (Row index not known)
更新类型化数据集中的现有记录(行索引未知)
Assign a specific DataRow to a variable using the generated FindBy method, and then use that variable to access the columns you want to edit and assign new values to them.
使用生成的 FindBy 方法将特定 DataRow 分配给变量,然后使用该变量访问要编辑的列并为其分配新值。
Dim Description As String = "Hello World Modified"
'Update DataTable
Dim Row As DataSet1.DataTableRow
Row = DataSet1.DataTableRow.FindByPrimaryKey(PK)
Row.Description = Description
Update existing records in untyped datasets (Row index not known)
更新无类型数据集中的现有记录(行索引未知)
Use the Select method of the DataTable to locate a specific row and assign new values to the desired columns
使用 DataTable 的 Select 方法定位特定行并为所需列分配新值
Dim Description As String = "Hello World Modified"
'Update DataTable
Dim Row() As Data.DataRow
Row = DataSet1.Tables("Table1").Select("PrimaryKey = '10'")
Row(0)("Description") = Description
Once this is done, I don't need to refresh anything else - my DataGridView has the latest information.
完成此操作后,我不需要刷新任何其他内容 - 我的 DataGridView 具有最新信息。