[Reserved] C # method of Merge merge data from two DataTable objects

In the Datatable C # class can be used to Merge method DataTable DataTable object class two identical structures are evaluated and set operations, and the combined data two rows DataTable DataTable object to the variable a, or to which a another object DataTable DataTable write object data for all rows. The following example uses the DataTable.Clone method, a method for configuration information assigned DataTable.Clone DataTable, including all DataTable schema and constraints.

Merge signature method is: void Merge (DataTable table); combined parameter table represents DataTable object variable.

By way of example as follows, the combined data of all objects to dataDt newDt1 subject, specific code as follows:

          DataTable dataDt = new DataTable();

            dataDt.Columns.Add(new DataColumn() { ColumnName = "Name" });
            dataDt.Columns.Add(new DataColumn() { ColumnName = "Id" });
            dataDt.Columns.Add(new DataColumn() { ColumnName = "Memo", DataType=typeof(String) });

            DataRow newRow = dataDt.NewRow();
            newRow["Name"] = "李四";
            newRow["Id"] = 22;
            newRow["Memo"] = "后续新增";
            dataDt.Rows.Add(newRow);  

            
            var newDt1 = dataDt.Clone();//通过Clone方法快速复制dataDt的结构信息

            DataRow inserDataRow = newDt1.NewRow();// Create a row DataRow objects according to the structure newDt1 of 
            inserDataRow [ "Name"] = "Wang five";
            inserDataRow [ "Id"] = 23 is;
            inserDataRow [ "Memo"] = "first line"; 
            newDt1.Rows.InsertAt (inserDataRow, 0); // write data into the row of objects newDt1 

            dataDt.Merge (newDt1); // the merged table data newDt1 to dataDt table.

After the program code is finished, we can see that there are two data dataDt in one of Name = "Joe Smith", the other is Name = "John Doe", i.e., the success of the combined data table to dataDt newDt1 table.

 

More DataTable operations can refer to the article:

(1) the difference between the DataTable C # class and the Copy method Clone

(2) C # framework created by the same method Clone rapid DataTable

(. 3) C # DataTable inserted into a specific position in the data by the method InsertAt

(. 4) C # quick copy objects through the Copy method DataTable

(. 5) C # Gets the index position of a column in the DataTable by IndexOf method

(. 6) C # Contains method by determining whether there is a column name in DataTable

(. 7) C # data manually into a new row at the end of the DataTable

(8) C # Gets all the column names by traversing the DataTable column

(. 9) C # DataTable in a column of data is removed by the Remove method

(10) C # how to add a data column to the DataTable

(. 11) C # properties acquired by the total number of rows Rows.Count

(12 is) C # through the data lines in the DataTable

(13) to create a DataTable C # and write data manually

 

Note: The text reproduced from personal bloggers station IT technology small fun house , the original link to merge two DataTable objects technical data _IT little fun house Merge method in C # .

Bloggers personal technology exchange group: 960 640 092, blogger micro-channel public numbers are as follows:

Guess you like

Origin www.cnblogs.com/xu-yi/p/11246483.html