C# implements Merge two tables. The two tables must have at least one common column as a connection item, otherwise the connection will lose its meaning. The following are the detailed codes for Mergeing two tables:
private void button1_Click(object sender, EventArgs e)//Button click trigger event { #region Table's Merge DataTable dt = new DataTable(); DataTable dt1 = new DataTable();//Create Table1 ("ID", typeof(string)); ("NAME",typeof(string)); ("AGE", typeof(int)); ("SEX", typeof(string)); = new DataColumn[] { ["ID"]}; for (int i = 0; i < 5; i++) { DataRow dr = (); dr["ID"] = "00" + (); dr["NAME"] = "00-" + (); dr["AGE"] = 15 + i; dr["SEX"] = "M"; (dr); } dt = dt1; DataTable dt2 = new DataTable();//Create Table2 ("ID", typeof(string)); ("NAME", typeof(string)); ("Course",typeof(string)); ("Score",typeof(int)); = new DataColumn[] { ["ID"] }; for (int i = 0; i < 5; i++) { DataRow dr = (); dr["ID"] = "00" + (); dr["NAME"] = "00-" + (); dr["Course"] ="C#"; dr["Score"] = i + 80; (dr); } dt = dt2; (dt2); //Copy var table1 = ();//Copy, copy table structure and data //Add a new line DataRow dr1 = (); dr1["ID"] = "005"; dr1["NAME"] = "00-5"; dr1["AGE"] = 15; dr1["SEX"] = "F"; (dr1,2);//Insert a newly added row at the specified position of the table (new DataColumn() { ColumnName = "Memo", DataType = typeof(string) });//Default insert to the last column ["Memo"].SetOrdinal(0);//Move the inserted column to the first row int memoIndex = ("Memo");//Get the column location information var isContainName = ("NAME");//Judge whether there is a column in the table List<string> columnsNameList = new List<string>();//Transfer to get all column names of table foreach (DataColumn col in ) { (); } (memoIndex);//Remove through column name index (2);//Remove through column name index ("SEX");//Remove through column names, it is recommended to make the latter string[] name = new string[];//Method 1: traversal output of the data in the table string[] id = new string[]; for (int i = 0; i < ; i++) { name[i] = [i]["NAME"].ToString(); id[i] = [i]["ID"].ToString(); } ();//Clear the data in the table //Clone var table2 = ();//Clone, copy the structure and constraint information of the table #endregion }
The above is the detailed content of Merge, Copy and Clone in C# implementing Table. For more information about C# table Merge, please follow my other related articles!