Several ways to add rows to a DataTable

Source: Internet
Author: User

When I recently made a project, I encountered adding a row of data from another table to an existing table. I do this with the following ideas:

DataTable dtsource = xxxx;//obtained data source

DataTable dttarget = Dtsource.clone ();//Get table structure

DTTARGET.ROWS.ADD (Dtsource.rows[0]. ItemArray);//The sentence is added directly to a new line

hahaha, it's easy!

In such a few words, can not gather enough for a blog to sprinkle, so the following reference a few examples on the web commonly used:

Method One: DataTable Tbldatas = new DataTable ("Datas");D atacolumn dc = NULL;DC = TblDatas.Columns.Add ("ID", Type.GetType (" System.Int32 "));d C. AutoIncrement = true;//automatically increases the DC. AutoIncrementSeed = 1;//start at 1DC. AutoIncrementStep = 1;//step is 1DC. AllowDBNull = FALSE;//DC = TblDatas.Columns.Add ("Product", Type.GetType ("System.String"));d C = TblDatas.Columns.Add (" Version ", Type.GetType (" System.String "));d C = tblDatas.Columns.Add (" Description ", Type.GetType (" System.String "));D Atarow NewRow; NewRow = Tbldatas.newrow (); newrow["Product"] = "fruit knife"; newrow["Version"] = "2.0"; newrow["Description"] = "fight Dedicated"; TblDatas.Rows.Add (newRow); newRow = Tbldatas.newrow (); NewRow ["Product"] = "folding stool"; newrow["Version"] = "3.0"; newrow["Description"] = "one of the seven arms of a walking lake"; TblDatas.Rows.Add (NewRow);     method Two: DataTable Tbldatas = new DataTable ("Datas"); TblDatas.Columns.Add ("ID", Type.GetType ("System.Int32") ); Tbldatas.columns[0]. AutoIncrement = True;tbldatas.columns[0]. AutoIncrementSeed = 1;tbldatas.columns[0]. AutoIncrementStep = 1;TBLDATAS.COLUMNS.ADD ("Product", Type.GetType ("System.String")), TblDatas.Columns.Add ("Version", Type.GetType (" System.String ") tblDatas.Columns.Add (" Description ", Type.GetType (" System.String ")); TblDatas.Rows.Add (New Object []{null, "A", "B", "C"}), TblDatas.Rows.Add (new object[] {null, "a", "B", "C"}); TblDatas.Rows.Add (new object[] {null, "a", "B", "C"}); TblDatas.Rows.Add (new object[] {null, "a", "B", "C"}), TblDatas.Rows.Add (new object[] {null, "a", "B", "C" });    This is commonly used on the web to add new row data to the table, you see the code naturally understand. And I won't say much. PS: This is the first time this cock blog, I hope you daniel/God spray Me Oh!    humbly ...

Several ways to add rows to a DataTable

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.