Dataset, able Replication
Datatable sourcetable;
Datatable objecttable;
Datatdatarow sourcerow;
Datatdatarow objectrow;
Datarow temprow;
Dataset soucedataset = new dataset ();
Copy Dataset
DataSet object = soucedataset. Copy (); // deep copy
DataSet object = soucedataset. Clone (); // copying architecture only
Copy datatable
Objecttable = sourcetable. Copy (); // deep copy
Objecttable = sourcetable. Clone (); // replication-only architecture
Copy datarow
This error is often encountered during project development-"This row already belongs to another table ". The statement that causes this error is as follows:
Objecttable. Rows. Add (sourcedatarow );
The reason is analyzed because datarow able is called by reference. Therefore, if a row is in one table, it cannot be added to another table.
Specific Method:
1 importrow method: Public void importrow (datarow );
Objecttable = sourcetable. Clone (); // you must first copy the schema of the table to have the same columns or relationships!
Foreach (datarow orow in sourcetable)
{
Objecttable. importrow (orow); // Add a new row to objecttable and copy the value of sourcerow. The table structure must be the same!
}
2. Loop every column of the datatable
Datarow adatarow = objecttable. newrow ();
Foreach (datacolumn adatacolumnyle = "color: Blue"> In sourcetable. columns)
{
Adatarow [adatacolumn. columnname] = sourcetable [I] [adatacolumn. columnname];
}
Objecttable. Rows. Add (adatarow );
3. Custom Replication
Objecttable. Columns. Add ("ID"); // you do not need to have the same architecture. just copy the columns you need!
Object [] myarry = new object [1];
Foreach (datarow orow in sourcetable)
{
Temprow = objecttable. newrow (); // This method must be called!
Myarry [0] = orow ["ID"]; // If the ID column in the source table does not exist in myarry, an error is returned!
Temprow. itemarray = myarry; // The itemarray attribute is an array of the object type.ProgramYou can copy data from multiple columns as needed!
Objecttable. Rows. Add (temprow); // This method must be called; otherwise, data in datarow cannot be displayed!
}
_____________________________________________________________________________________________________
4. loaddatarow method: Public datarow loaddatarow (object [] values, bool facceptchanges );
Object [] newrow = new object [3];
// Set the object array value
Newrow [0] = "hello ";
Newrow [1] = "world ";
Newrow [2] = "two ";
Datarow myrow;
Objecttable. beginloaddata ();
// Add a new row to the table
Myrow = objecttable. loaddatarow (newrow, true); // The flag must be set to true to add a new row.
Objecttable. endloaddata ();
For your security, please only open the URL with reliable source
Cancel website opening
From: http://hi.baidu.com/zhendong7799/blog/item/24a34594d774ae047bf480a7.html