DataTable copies itself rows

Source: Internet
Author: User

In the course of our work, it is possible to use a DataTable to produce some duplicate data (without repeatedly reading the database)

No nonsense, directly on the code

The DataTable replicates its own row (for the purpose of generating duplicate data), which has been tested by directly replicating

1  /// <summary>2         ///Create a DataTable and generate test data3         /// </summary>4         /// <returns></returns>5          Public StaticDataTable Createdtdata (intargrownum)6         {7DataTable dt =NewDataTable ();8Dt. Columns.Add ("ID");9Dt. Columns.Add ("Name");TenDt. Columns.Add ("Sex"); OneDt. Columns.Add (" Age"); A  -             inti =0; -              for(; i < argrownum; i++) the             { -DataRow dr =dt. NewRow (); -dr[0] =i; -dr[1] ="LD"+i; +dr[2] ="male"; -dr[3] = i *6; + dt. Rows.Add (DR); A             } at  -             returnDT; -         } - Static voidMain (string[] args) -         { -Console.WriteLine ("1. Create a DataTable 10 row"); inDataTable Dtsource = Createdtdata (Ten); -Console.WriteLine ("2. Copy data by default is the first line"); toDtsource.importrow (dtsource.rows[0]); +Dtsource.rows[dtsource.rows.count-1][1] ="Test"; -  theConsole.WriteLine (dtsource.rows[0][1]. ToString ()); *Console.WriteLine (Dtsource.rows[dtsource.rows.count-1][1]. ToString ()); $ Panax Notoginseng Console.read (); -}
View Code

Note:

The main idea is to use a DataTable function ImportRow (the function is to copy a row, not copy the reference, is to copy the data);

where Dtsource.importrow (Dtsource.rows[0]), can be replaced by DTSOURCE.ROWS.ADD (Dtsource.rows[0]. ItemArray);

Reference: http://blog.sina.com.cn/s/blog_75a555e401014rid.html

DataTable copies itself rows

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.