Recently, the project was too busy and there was almost no time to patronize it.
In addition to implementing its own modules, the project also has a team leader which is not competent.
Because the module is responsible for a lot of data operations, this error is often encountered, "This row already belongs to another table" error.
The statement that causes this error is as follows:
Dtpricetable. Rows. insertat (adatarow, I );
Or
Dtpricetable. Rows. Add (adatarow );
I analyzed the cause because datarow datatable is called by reference. Therefore, if a row is in one table, it cannot be added to another table.
There are two specific improvement strategies:
1. assign values to each column of the original datarow. As follows:
Datarow temprow = dtitemprice. newrow ();
Temprow ["first"] = adatarow ["first"]
Temprow ["second"] = adatarow ["second"]
Temprow ["third"] = adatarow ["third"]
Temprow ["forth"] = adatarow ["forth"]
Temprow ["th"] = adatarow ["th"]
......
Dtpricetable. Rows. Add (temprow)
2. A colleague reminded me why every column of the able is not cyclically stored? I guess so. Why not!
Well, no matter how many columns are in the table, you only need three sentences.
Datarow adatarow = dtitemprice. newrow ();
Foreach (datacolumn adatacolumn in dtitemprice. columns)
{
Adatarow [adatacolumn. columnname] = dritemprices [I] [adatacolumn. columnname];
}
Insertconditiontype (adatarow, dtitemprice );
Test.
Later, I checked that both dataset and able have a method: Copy () to copy the structure and data of the system. Data. datatable.
Then, using this function, both dataset, able, and datarow can be copied.