Dataview view = new dataview ();
View.
Table= Datatablea;
View. rowfilter = "itemtype = 'book'"; // itemtype is a field in ableablea.
Datatableb = view. totable ();
Or:
Datarow [] rows = ableablea. Select ("itemtype = 'book '");
Datatableb = datatablea. Clone ();
Foreach (datarow row in rows)
{
Datatableb. importrow (ROW );
}
Or
/// Execute the query in the datatable to return the new datatable
/// </Summary>
/// <Param name = "DT"> source data datatable </param>
/// <Param name = "condition"> query condition </param>
/// <Returns> </returns>
Private datatable getnewdatatable (datatable DT, string condition, string sortstr)
{
Datatable newdt = new datatable ();
Newdt = DT. Clone ();
Datarow [] DR = DT. Select (condition, sortstr );
For (INT I = 0; I <dr. length; I ++)
{
Newdt. importrow (datarow) Dr [I]);
}
Return newdt; // The returned query result.
}
Or column by Column
Public static int tabledataexchange (Dataset ds, string tablename, datatable sourcedt)
{
For (INT I = 0; I <sourcedt. Rows. Count; I ++)
{
Datarow drnew = Ds. Tables [tablename]. newrow ();
Foreach (datacolumn DC in sourcedt. columns)
{
Drnew [DC. columnname] = sourcedt. Rows [I] [DC. columnname];
}
DS. Tables [tablename]. Rows. Add (drnew );
}
DS. Tables [tablename]. acceptchanges ();
Datatable dt = Ds. Tables [tablename];
Return Ds. Tables [tablename]. Rows. count;
}
Or
Dataset objects support ADO. the core object of the net disconnected and distributed data solution, which is widely used. we often need to use the data, such as getting data from a able, copying data from another Abe, or data from datarow. However, only the replication of dataset and datatable supports deep replication, that is to say, the structure of the element can be copied, and the data of the element can be copied. The datatdatarow has no relevant replication method. The following describes the replication of these data elements.
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 adatacolumn 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. You can copy the data of 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 ();
This method is complex. We recommend that you do not use it if you simply copy data from existing rows to add new rows. For more information about the usage, see the SDK document.
Or:
<Type = "text/JavaScript"> alimama_pid = "plain"; alimama_titlecolor = "0000ff"; expires = "000000"; alimama_bgcolor = "ffffff"; alimama_bordercolor = "e6e6e6 "; alimama_linkcolor = "008000"; alimama_bottomcolor = "ffffff"; alimama_anglesize = "0"; bandwidth = "0"; alimama_icon = "0"; bandwidth = "16"; alimama_width = 658; alimama_height = 60; alimama_type = 2; <src = "http://a.alimama.cn/inf.js" type = text/JavaScript>
We often need to add a row of data to the datatable. In most cases, we add some data collected from the UI controls and program variables to the datatable. Add the Supplier Code and name to the able as follows:
Datatable dtprovider = new datatable ();
Datarow drrow = dtprovider. newrow ();
Drrow [0] = txtprovidercode. Text. Trim ();
Drrow [1] = txtprovidername. Text. Trim ();
Dtprovider. Rows. Add (drrow );
In most cases, these statements fully meet the requirements. However, if you want to add a row of another datatable with the same structure to this dtprovider, you cannot simply add it. Otherwise, an error "this row belongs to another table." is displayed .". At this time, we must define another datarow, assign the data of the source datarow to the destination datarow, and then add the data to the able. As follows:
Datatable dtprovider = new datatable ();
Datarow drtarget = dtprovider. newrow ();
Drtarget. itemarry = drsource. itemarry;
// Note: The drsource is a row in another datatable with the same structure.
Dtprovider. Rows. Add (drtarget );
<Type = "text/JavaScript"> alimama_pid = "Courier"; alimama_type = "F"; alimama_sizecode = "tl_1x5_8"; alimama_fontsize = 12; alimama_bordercolor = "ffffffff "; alimama_bgcolor = "ffffff"; alimama_titlecolor = "0000ff"; alimama_underline = 0; alimama_height = 22; alimama_width = 0; <src = "http://a.alimama.cn/inf.js" type = text/JavaScript>