将一个datatable的值赋给另一个dt的三种方法--转

将一个datatable的值赋给另一个datatable的三种方法总结:

 

方法一:new一个新行,给每个列去赋值。

              DataTable dt1 = new DataTable();
              DataTable dt2 = new DataTable();

              DataRow NewRow = dt2.NewRow();
              NewRow["ID"] = dt1.Rows[i]["ID"].ToString();
              NewRow["No"] = dt1.Rows[i]["No"].ToString();
              NewRow["Name"] = dt1.Rows[i]["Name"].ToString();

              dt2.Rows.Add(NewRow);

 

方法二:克隆DataTable的表结构,再进行填值

               DataTable dtCloneTable = new DataTable("NodeInfo");
               dtCloneTable = dtInfo.Clone();
               foreach (DataRow r in dtInfo.Rows)
               {
                 DataRow r2 = dtCloneTable.NewRow();
                 r2.ItemArray = r.ItemArray;
                 dtCloneTable.Rows.Add(r2);
                }

方法三:直接复制datatable

            dt2 = dt1.Copy();

你可能感兴趣的:(Datatable)