简体   繁体   中英

Adding data to a row in a Dataset?

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")

在此处输入图片说明

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);

Just get a new Row from the DataTable and then add that row to the table, Use DataTable.NewRow method

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:

ds.Tables[0].Rows.Add(ArrayProperties[0].Value,ArrayProperties[1].Value,ArrayProperties[2].Value,ArrayProperties[3].Value);

The columns collection of a Datatable regards the table structure. In your code you mix adding columns and populating fileds.

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 article for more details.

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM