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