DataTable_A:
物料号   数量
MAT_01   111
MAT_02   222
MAT_03   333
...DataTable_B:
物料号   物料名称
MAT_01   MAT_01名称
MAT_02   MAT_02名称
MAT_03   MAT_03名称
...想通过DataTable_A+DataTable_B,得到以下结果(赋值给DataTable_C)
物料号   数量   物料名称
MAT_01   111    MAT_01名称
MAT_02   222    MAT_02名称
MAT_03   333    MAT_03名称
...
请问怎么实现?

解决方案 »

  1.   

    如果数据集中的表有逻辑关系,则 DataRelation 对象可使另一个表中的相关记录供您使用。包含具有 Customers 表(主键为 CustomerID)和 Orders 表(外键为 CustomerID)的客户数据库的数据集即属于此例。可以创建 DataRelation 对象,并将其属性设置为反映这些键。然后可以使用 DataRelation 对象获取相关记录。此过程是间接的,因为并未联接表,而是调用父表中数据行的 GetChildRows 方法,给它传递定义父/子关系的 DataRelation 对象。该方法返回相关子记录的数组。下列代码阐释一个获取相关记录的简单示例。本例中,drarray 数组设置为 Customers 表中第一行的子记录。' Visual Basic
    Dim RowCtr As Integer
    ' Dim an array of datarows to hold the child records.
    Dim drarray() As DataRow
    ' GetChildRows gets related rows. It is a method on the datatable, and
    ' takes a DataRelation name as a string.
    RowCtr = 0
    drarray = dsCustomersOrders1.Customers(RowCtr).GetChildRows("CustomersOrders")// C#
    int rowCtr;
    // Declare an array of datarows to hold the child records.
    DataRow[] drarray;
    // GetChildRows gets related rows. It is a method on the datatable, and
    // takes either DataRelation object name as a string.
    rowCtr = 0;
    drarray = dsCustomersOrders1.Customers[rowCtr].GetChildRows("CustomersOrders");
    同样,可以通过调用子表中数据行的 GetParentRow 方法获取给定子记录的父行。这种情况下,该方法不返回数组,而是返回单个数据行。有关更多信息,请参见导航表间关系。
      

  2.   


    增加一个动态表,增加三列
    前面省略。。
    DataTable dt = new DataTable();
    dt.Columns.Add["物料号"]
    dt.Columns.Add["数量"]
    dt.Columns.Add["物料名称"]
    for(int i=0;i<dt.row.count;i++)
    {
    dt.Rows[i]["数量"] = "需要的值";
    ...
    ..}
      

  3.   

    我希望的是,有没有不需要循环的方法?比如:有没有这种方法(SELECT.DataTable_A.物料号,DataTable_A.数量,DataTable_B.物料名称 WHERE DataTable_A.物料号 = DataTable_B.物料号)
    注:DataTable_A,DataTable_B 在同一个DATASET 里.因我看书上说,DATASET = 数据库,DataTable = 数据表.所以想看看有没有这种方法.
      

  4.   

    select DataTable_A.物料号 ,DataTable_A.数量,DataTable_B.物料名称 from DataTable_A,DataTable_B where DataTable_A.物料号=DataTable_B.物料号然后查询就可以读取字段了