Copy a row in the able to another new datatable)

Source: Internet
Author: User
=== Premise: we already have a datatable data table. All we need now is a row of Data ===

For example, the following sectionCodeIn a method, a datatable is returned!

Sqlcon = New Sqlconnection (strcon );
Sqlcom = New Sqlcommand ();
Sqlcom. Connection = Sqlcon;
Sqlcom. commandtext = " Select_v " ;
Sqlcom. commandtype = Commandtype. storedprocedure;
Sqlcon. open ();
SDR = Sqlcom. executereader ();
DT = New Datatable ( );
DT. Load (SDR );

Assume that the returned datatable is DT,
Method 1: Copy A able, clear the data (the column structure is retained), and add the required row from DT using the importrow () method;

Datatable dt2 = new datatable ();
Dt2 = DT. Copy ();
Dt2.rows. Clear ();
Dt2.importrow (Dt. Rows [0]); // This is the first line added

Method 2:

First, the new able must be added to the corresponding column name (column)
An empty and non-structured able cannot directly add a row to it!

For ( Int I = 0 ; I < DT. Columns. Count; I ++ )
{
Dt2.columns. Add (Dt. Columns [I]. columnname); // you can add the column data type to the overload method.
}

Then you can join the row directly!

// 1
Datarow drq = Dt2.newrow ();
Drq. itemarray = DT. Rows [ 0 ]. Itemarray; // This is the first line added
Dt2.rows. Add (drq );
// 2
Dt2.importrow (Dt. Rows [ 0 ]); // This is the first line added

Finally, you can put the new able into Dataset:
DS = new dataset ();
DS. Tables. Add (dt2 );

 

A simple method to filter repeated rows of a rows table

Dataview View=Table. defaultview;
Datatable tagettable=View. totable (True,"Column1","Column2",...);

Source Program Resources from the network are recorded for future use. May be useful to everyone
// Table datatablesource to obtain data
String connectionstring = "provider = Microsoft. Jet. oledb.4.0; persist Security info = false;" +
"Data Source = D: \ vb60 \ MDB \ northwind. mdb ";
Oledbconnection conn = new oledbconnection (connectionstring );
Conn. open ();
Oledbdataadapter Ada = new oledbdataadapter ("select * from orders", Conn );
Datatable datatablesource = new datatable ();
Ada. Fill (datatablesource );
Datagrid1.datasource = datatablesource;
// Create a new table datatabledest
Datatable datatabledest = new datatable ();
// Copy the structure of the datatablesource table to the datatabledest table.
Datatabledest = datatablesource. Clone ();
// Copy the data to the new table.
Foreach (datarow DR in datatablesource. Rows)
{
// Use importrow () to copy data. If ableabledest. Rows. Add (DR) is used, an error occurs: system. argumentexception: This row already belongs to another table.
Datatabledest. importrow (DR );
}

Datagrid2.datasource = datatabledest;

Conn. Close ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.