.net 向数据集添加行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/417133/
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
add rows to a dataset
提问by user26087
Is it possible to add rows to a dataset?
是否可以向数据集添加行?
回答by Stephen Wrighton
yes.
是的。
dim row as DataRow
row = ds.tables(0).NewRow
' Add Values to Row here
ds.tables(0).rows.add(row)
回答by David
No. But I suspect you mis-worded your question.
不,但我怀疑你的问题措辞有误。
You can add rows to a dataTABLE. A Dataset is made up of DataTables (or is empty).
您可以向数据表中添加行。数据集由数据表组成(或为空)。
In order to add rows to a DataTable, you first make the DataRow and then .Add it to the DataTable
为了向 DataTable 添加行,首先创建 DataRow,然后将其添加到 DataTable
回答by lc.
Yes, you can certainly add rows to the datatables in a dataset.
是的,您当然可以向数据集中的数据表添加行。
Check this MSDN pagefor a how-to. It covers both strongly typed datasets and untyped datasets.
查看此 MSDN 页面以获取操作方法。它涵盖了强类型数据集和非类型数据集。
To add a new record to a typed dataset
Declare a new instance of the typed dataset. In the following example, you declare a new instance of the CustomersRow class, assign it a new row, populate the columns with data, and add the new row to the Customers table's Rows collection:
Dim newCustomersRow As NorthwindDataSet.CustomersRow newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow() newCustomersRow.CustomerID = "ALFKI" newCustomersRow.CompanyName = "Alfreds Futterkiste" NorthwindDataSet1.Customers.Rows.Add(newCustomersRow)To add a record to an untyped dataset
Call the NewRow method of a DataTable to create a new, empty row. This new row inherits its column structure from the data table's DataColumnCollection. The following code creates a new row, populates it with data, and adds it to the table's Rows collection.
Dim newCustomersRow As DataRow = DataSet1.Tables("Customers").NewRow() newCustomersRow("CustomerID") = "ALFKI" newCustomersRow("CompanyName") = "Alfreds Futterkiste" DataSet1.Tables("Customers").Rows.Add(newCustomersRow)
向类型化数据集添加新记录
声明类型化数据集的新实例。在以下示例中,您声明了 CustomersRow 类的新实例,为其分配了一个新行,用数据填充列,并将新行添加到 Customers 表的 Rows 集合中:
Dim newCustomersRow As NorthwindDataSet.CustomersRow newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow() newCustomersRow.CustomerID = "ALFKI" newCustomersRow.CompanyName = "Alfreds Futterkiste" NorthwindDataSet1.Customers.Rows.Add(newCustomersRow)向无类型数据集添加记录
调用 DataTable 的 NewRow 方法来创建一个新的空行。这个新行从数据表的 DataColumnCollection 继承其列结构。以下代码创建一个新行,用数据填充它,并将其添加到表的 Rows 集合中。
Dim newCustomersRow As DataRow = DataSet1.Tables("Customers").NewRow() newCustomersRow("CustomerID") = "ALFKI" newCustomersRow("CompanyName") = "Alfreds Futterkiste" DataSet1.Tables("Customers").Rows.Add(newCustomersRow)
回答by GWLlosa
You can add rows to a datatable, which can be contained in a dataset. Call the 'Add' function on the "Rows" collection of the table.
您可以向数据表中添加行,该数据表可以包含在数据集中。在表的“行”集合上调用“添加”函数。
回答by Brad Moulder
Try adding rows to your dataset like this:
尝试将行添加到您的数据集中,如下所示:
NewRow = PreviousYear.Tables(0).Rows.Add
NewRow(0) = Your Value
NewRow(1) = Your Value
NewRow(2) = Your Value
NewRow(3) = Your Value
NewRow(4) = Your Value
This code will add 1 row with 5 columns.
此代码将添加 1 行 5 列。
回答by Pete Ottoson
These solutions are good, However if you simply use ComboBox.SelectedIndex = -1 then the display will be blank and you don't need a "blank" row in the first place.
这些解决方案很好,但是如果您只是使用 ComboBox.SelectedIndex = -1 那么显示将是空白的并且您首先不需要“空白”行。

