C# 将数据添加到数据集中的一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12510291/
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
Adding data to a row in a Dataset?
提问by Pomster
I am trying to add data to a row in a dataset but the data is always on a new row?
我正在尝试将数据添加到数据集中的一行,但数据总是在新行上?
I need the data to populate under its column. I need something like Ds.Tables[0].Rows[1].add("Item")
我需要在其列下填充数据。 I need something like Ds.Tables[0].Rows[1].add("Item")


This is how i am inserting the data:
这是我插入数据的方式:
DataSet ds = new DataSet();
ds.Tables.Add("Properties");
//GPS
ds.Tables[0].Columns.Add(ArrayProperties[0].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[0].Value);
//Street Num and Name
ds.Tables[0].Columns.Add(ArrayProperties[3].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[3].Value);
//Suburb
ds.Tables[0].Columns.Add(ArrayProperties[6].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[6].Value);
//City
ds.Tables[0].Columns.Add(ArrayProperties[7].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[7].Value);
//Province
ds.Tables[0].Columns.Add(ArrayProperties[8].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[8].Value);
//Locality Map
ds.Tables[0].Columns.Add(ArrayProperties[9].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[9].Value);
//Property Type
ds.Tables[0].Columns.Add(ArrayProperties[10].FormMobiField);
ds.Tables[0].Rows.Add(ArrayProperties[10].Value);
采纳答案by Habib
Just get a new Row from the DataTable and then add that row to the table, Use DataTable.NewRowmethod
只需从 DataTable 中获取一个新行,然后将该行添加到表中,使用DataTable.NewRow方法
DataRow dr = ds.Tables[0].NewRow();
dr["Column1"] = "value";
dr["Column2"] = "value";
dr["Column3"] = "value";
ds.Tables[0].Rows.Add(dr);
You are adding row after adding each column, You may first create your data table's structure by adding all the columns and then you can get the new row using DataTable.NewRow()and later you can add that row to your data table. After adding all the columns you may also try:
您在添加每一列后添加行,您可以首先通过添加所有列来创建数据表的结构,然后您可以获得新行,然后您可以将该行DataTable.NewRow()添加到数据表中。添加所有列后,您还可以尝试:
ds.Tables[0].Rows.Add(ArrayProperties[0].Value,ArrayProperties[1].Value,ArrayProperties[2].Value,ArrayProperties[3].Value);
回答by Alberto De Caro
The columns collection of a Datatable regards the table structure. In your code you mix adding columns and populating fileds.
Datatable 的列集合与表结构有关。在您的代码中,您混合添加列和填充字段。
You should first create the structure (not tested and syntax errors can occur):
您应该首先创建结构(未经测试,可能会出现语法错误):
Dataset ds = new Dataset();
Datatable dt = new Datatable();
dt.columns.add(new Column.add(...));
...
dt.columns.add(new Column.add(...));
ds.Tables.add(dt);
And then:
进而:
Datarow r = ds.tables[0].NewRow();
r["column1"] = value1;
...
r["columnX"] = valueX;
ds.Tables[0].rows.add(r);
See this msdn articlefor more details.
有关更多详细信息,请参阅此msdn 文章。
回答by Haris Iltifat
Add the columns as you are adding. For populating rows, do the below.
在添加时添加列。要填充行,请执行以下操作。
foreach (DataRow row in ds.Tables[0]) // Loop over the rows.
{
row[ArrayProperties[i].FormMobiField]=ArrayProperties[0].Value;
i++;
}
If it doesn't work then let me know,
如果它不起作用,请告诉我,

