不是简单的合并,比如table1的字段为:
UserId  Password Table2的字段为:
UserId Tel如何将这两个表合并成这样一张表:
UserId Password Tel由于这两张表一个是通过WebService取来的一个是访问本地数据库的,所以合并只能在C#中完成。

解决方案 »

  1.   

    在Table中最加列,然后对起赋值即可
      

  2.   

    DataTable t1 = new DataTable();
    DataTable t2 = new DataTable();
    // t1 , t2 获取数据...
    t2.Columns.Add("Password");
    for(int i = 0 ; i < t1.Rows.Count ; i ++)
    {
      t2.Rows[i]["Password"] = t1.Rows[i]["Password "];
    }
    这样试试
      

  3.   

    .net1.1 方法:string sqlAllCustomers = "SELECT * FROM Customers";
    string cnStr = @"Data Source=.;Initial Catalog=northwind;Integrated Security=True"; using (SqlConnection cn = new SqlConnection(cnStr))
    {
        cn.Open();
        SqlCommand cmd = new SqlCommand(sqlAllCustomers, cn);
        SqlDataAdapter adpt = new SqlDataAdapter(cmd);
        DataTable dtCust1 = new DataTable("Customers");
        adpt.Fill(dtCust1);
        dtCust1.PrimaryKey = new DataColumn[]{dtCust1.Columns["CustomerID"]};
         DataTable dtCust2 = dtCust1.Clone();
        DataRow row1 = dtCust2.NewRow();
        row1["CustomerID"] = "ALFKI";
        row1["CompanyName"] = "Some Company";
        dtCust2.Rows.Add(row1);    DataRow row2 = dtCust2.NewRow();
        row2["CustomerID"] = "FOO";
        row2["CompanyName"] = "Some Other Company";
        dtCust2.Rows.Add(row2);    DataSet ds = new DataSet("MySillyDataSet");
        ds.Tables.Add(dtCust1);
        ds.Merge(dtCust2);    dgTest.DataSource = dtCust1;
    }.net2.0方法
    在ADO.NET 2.0中,对DataTable作了改进,同样提供了Merge方法。因此如上的代码中,如果要合并表dtCust1和dtCust2,就不必再引入DataSet对象了:
    dtCust1.Merge(dtCust2);
      

  4.   

    DataTable.Merge 
    http://msdn2.microsoft.com/zh-cn/library/system.data.datatable.merge.aspx
      

  5.   

    public class Form1 : System.Windows.Forms.Form???? {???????? private DataAccess _dataAccess;???????? private DatasetOrders _ds;???????? //……???????? //构造函数???????? public Form1()???????? {????????????? InitializeComponent();????????????? _dataAccess = new DataAccess();????????????? _ds = new DatasetOrders();????????????? _ds.EnforceConstraints = false; //关闭约束检查,提高数据填充效率????????????? this.dataGridCustomers.DataSource = _ds;????????????? this.dataGridCustomers.DataMember = _ds.Customers.TableName;????????????? this.dataGridOrders.DataSource = _ds;????????????? this.dataGridOrders.DataMember = _ds.Customers.TableName "." _ds.Customers.ChildRelations[0].RelationName;????????????? this.dataGridOrderDetails.DataSource = _ds;????????????? this.dataGridOrderDetails.DataMember = _ds.Customers.TableName "." _ds.Customers.ChildRelations[0].RelationName "." _ds.Orders.ChildRelations[0].RelationName;???????? }对于上面的三个表的动态关联,你也可以使用SetDataBinding方法来完成数据的动态绑定,而不是分别指定DataGride的DataSource和DataMemger属性。this.dataGridCustomers.SetDataBinding(_ds,_ds.Customers.TableName);this.dataGridOrders.SetDataBinding(_ds,_ds.Customers.TableName "." _ds.Customers.ChildRelations[0].RelationName);this.dataGridOrderDetails.SetDataBinding(_ds,_ds.Customers.TableName "." _ds.Customers.ChildRelations[0].RelationName "." _ds.Orders.ChildRelations[0].RelationName);}数据填充事件处理如下:???????????????????????? private void buttonFillData_Click(object sender, System.EventArgs e)???????? {????????????? _ds.Clear();//重新填充数据集????????????? _dataAccess.FillCustomerOrdersInfo(_ds);????????????? //_dataAccess.FillCustomerOrdersInfoWithSqlHelper(_ds);???????? }执行上面的事
      

  6.   

    下面的控制台应用程序创建一个简单的 DataTable 并将数据添加到该表中。该示例随后创建该表的一个副本,并向该副本添加行。最后,该示例调用 Merge 方法将第二个表中的数据与第一个表中的数据合并。Visual Basic 复制代码Private Sub DemonstrateMergeTable()
      Dim table1 As New DataTable("Items")  ' Add columns
      Dim column1 As New DataColumn("id", GetType(System.Int32))
      Dim column2 As New DataColumn("item", GetType(System.Int32))
      table1.Columns.Add(column1)
      table1.Columns.Add(column2)  ' Set the primary key column.
      table1.PrimaryKey = New DataColumn() {column1}  ' Add RowChanged event handler for the table.
      AddHandler table1.RowChanged, AddressOf Row_Changed  ' Add some rows.
      Dim row As DataRow
      For i As Integer = 0 To 3
        row = table1.NewRow()
        row("id") = i
        row("item") = i
        table1.Rows.Add(row)
      Next i  ' Accept changes.
      table1.AcceptChanges()
      PrintValues(table1, "Original values")  ' Create a second DataTable identical to the first.
      Dim table2 As DataTable = table1.Clone()  ' Add three rows. Note that the id column can't be the 
      ' same as existing rows in the original table.
      row = table2.NewRow()
      row("id") = 14
      row("item") = 774
      table2.Rows.Add(row)  row = table2.NewRow()
      row("id") = 12
      row("item") = 555
      table2.Rows.Add(row)  row = table2.NewRow()
      row("id") = 13
      row("item") = 665
      table2.Rows.Add(row)  ' Merge table2 into the table1.
      Console.WriteLine("Merging")
      table1.Merge(table2)
      PrintValues(table1, "Merged With table1")End SubPrivate Sub Row_Changed(ByVal sender As Object, _
      ByVal e As DataRowChangeEventArgs)
      Console.WriteLine("Row changed {0}{1}{2}", _
        e.Action, ControlChars.Tab, e.Row.ItemArray(0))
    End SubPrivate Sub PrintValues(ByVal table As DataTable, _
      ByVal label As String)
      ' Display the values in the supplied DataTable:
      Console.WriteLine(label)
      For Each row As DataRow In table.Rows
        For Each col As DataColumn In table.Columns
          Console.Write(ControlChars.Tab + " " + row(col).ToString())
        Next col
        Console.WriteLine()
      Next row
    End SubC# 复制代码private static void DemonstrateMergeTable()
    {
        DataTable table1 = new DataTable("Items");    // Add columns
        DataColumn column1 = new DataColumn("id", typeof(System.Int32));
        DataColumn column2 = new DataColumn("item", typeof(System.Int32));
        table1.Columns.Add(column1);
        table1.Columns.Add(column2);    // Set the primary key column.
        table1.PrimaryKey = new DataColumn[] { column1 };    // Add RowChanged event handler for the table.
        table1.RowChanged += 
            new System.Data.DataRowChangeEventHandler(Row_Changed);    // Add some rows.
        DataRow row;
        for (int i = 0; i <= 3; i++)
        {
            row = table1.NewRow();
            row["id"] = i;
            row["item"] = i;
            table1.Rows.Add(row);
        }    // Accept changes.
        table1.AcceptChanges();
        PrintValues(table1, "Original values");    // Create a second DataTable identical to the first.
        DataTable table2 = table1.Clone();    // Add three rows. Note that the id column can't be the 
        // same as existing rows in the original table.
        row = table2.NewRow();
        row["id"] = 14;
        row["item"] = 774;
        table2.Rows.Add(row);    row = table2.NewRow();
        row["id"] = 12;
        row["item"] = 555;
        table2.Rows.Add(row);    row = table2.NewRow();
        row["id"] = 13;
        row["item"] = 665;
        table2.Rows.Add(row);    // Merge table2 into the table1.
        Console.WriteLine("Merging");
        table1.Merge(table2);
        PrintValues(table1, "Merged With table1");}private static void Row_Changed(object sender, 
        DataRowChangeEventArgs e)
    {
        Console.WriteLine("Row changed {0}\t{1}", 
            e.Action, e.Row.ItemArray[0]);
    }private static void PrintValues(DataTable table, string label)
    {
        // Display the values in the supplied DataTable:
        Console.WriteLine(label);
        foreach (DataRow row in table.Rows)
        {
            foreach (DataColumn col in table.Columns)
            {
                Console.Write("\t " + row[col].ToString());
            }
            Console.WriteLine();
        }
    }
      

  7.   

    每个Table的数据可能上万,所以需要一个效率高一点的方法
      

  8.   

    原先是关联这两张表然后绑定到DataGrid,实现没问题,可是显示的样式很难调整,因为子DataGrid里不止一个字段,而子DataGrid的内容都是显示在父Datagrid的一个字段里。所以子Datagrid的字段名无法显示。
      

  9.   

    楼上累的要死的同志没明白我的意思,两张表是不同架构的,只不过Table1与Table2存在外键关系。
      

  10.   

    有点明白又不确定,你的意思是: UserId Password Tel1 Tel2 Tel3显示OR
    UserId Password Tel1
            Tel2
            Tel3 
    哪种是你需要的?
      

  11.   

    即相当于对table1和table2执行left join outer操作