我有两个datatable table1,table2, 他们两个的column都相同,我想用c#代码 把table2的内容接到table1后面
应该怎么写?

解决方案 »

  1.   

    datetable好像有个合并的函数吧,Merge?
      

  2.   


            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("客户A", typeof(string));        dt.Rows.Add(1, "A1");
            dt.Rows.Add(2, "A2");
            dt.Rows.Add(3, "A3");
            dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };        DataTable dt2 = new DataTable();
            dt2.Columns.Add("ID", typeof(int));
            dt2.Columns.Add("客户B", typeof(string));        dt2.Rows.Add(1, "B1");
            dt2.Rows.Add(2, "B2");
            dt2.Rows.Add(4, "B4");
            dt2.PrimaryKey = new DataColumn[] { dt2.Columns["id"] };
         
          
            dt.Merge(dt2);        this.DataGrid1.DataSource = dt.DefaultView;
            this.DataGrid1.DataBind();
      

  3.   

    我只知道用SQl语句
    Select * from table1的数据库的表 Union Select * from table2的数据库的表然后查询得到的结果就是 table2的内容接到table1后面
      

  4.   

    如果两个表格Column一样。
    table1.Merge(table2)