How to copy data rows from one DataTable to another

Source: Internet
Author: User

DataTable dt = "";  Here is the DataTable data that fills the datatable dtnew = dt. Copy ();  Copy DT table data structure dtnew.clear (  )<  DT. Rows.Count; i++)        {            if (conditional statement)            {                dtNew.Rows.Add (dt. Rows[i]. ItemArray);  Add data row            }        }

= = = Premise: We already have a DataTable of data that is needed just one row of data = = =

For example, the following code returns a datatable! in a method.

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

If the returned DataTable is DT,
Method One: Directly copy a DataTable, and then empty the data (column structure is also retained), and finally use the ImportRow () method to add the required row from the DT;

            DataTable DT2 = new DataTable ();            DT2 = dt. Copy ();            DT2. Rows.clear ();            DT2. ImportRow (dt. Rows[0]);//This is the first line to join.

Method Two:

First the new DataTable must include the corresponding column name (column)
An empty, unstructured DataTable is not allowed to add a line directly to it!

             < DT . Columns.count; i++)             {                 DT2. Columns.Add (dt. Columns[i]. ColumnName);//There are overloaded methods that can be added to the type of column data             }

Then you can join the row directly!

1             DataRow DRQ = dt2. NewRow ();             Drq. ItemArray = dt. Rows[0]. itemarray;//This is the first line of DT2 to join             . Rows.Add (DRQ);//2             DT2. ImportRow (dt. Rows[0]);//This is the first line to join.

Finally, you can put the new DataTable in the dataset:
ds = new DataSet ();
Ds. Tables.add (DT2);

Simple way to filter rows of a DataTable by repeating rows

DataView view = table. DefaultView;
DataTable tagettable= view. ToTable (True, "Column1", "Column2", ...);

The source program comes from resources on the network and I record it for later use. may be useful to everyone//table Datatablesource get 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 table Datatablesource to the new table datatabledest datatabledest = Datatablesource.clone (); Then copy the data to the new table in the foreach (DataRow dr in Datatablesource.rows) {//using the ImportRow () method to copy the data.            If you use DATATABLEDEST.ROWS.ADD (DR), you will get an error: System.ArgumentException: The row already belongs to another table.           Datatabledest.importrow (DR);  } Datagrid2.datasource = Datatabledest;         Conn.close (); 

How to copy data rows from one DataTable to another

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.