DataColumn parentCol;
    DataColumn childCol;
    // Code to get the DataSet not shown here.
    parentCol = DataSet1.Tables["Customers"].Columns["CustID"];
    childCol = DataSet1.Tables["Orders"].Columns["CustID"];
    // Create DataRelation.
    DataRelation relCustOrder;
    relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol);
    // Add the relation to the DataSet.
    DataSet1.Relations.Add(relCustOrder);

解决方案 »

  1.   

    private void GetParentRowForTable(DataTable thisTable, DataRelation relation){
       if(thisTable ==null){return;}
       // For each row in the table, print column 1 of the parent DataRow.
       DataRow parentRow;
       foreach(DataRow r in thisTable.Rows){
          parentRow = r.GetParentRow(relation);
          Console.Write("\t child row: " + r[1]);
          Console.Write("\t parent row: " + parentRow[1]+ "\n");
       }
    }
      

  2.   

    这段代码我在SDK中也看到了,但是
    1.传给我的参数只有DataSet
    2.我是在静态DataSet中建立的Relation
      

  3.   

    parentCol = DataSet1.Tables["Customers"].Columns["CustID"];
    childCol = DataSet1.Tables["Orders"].Columns["CustID"];
    --->>>你的DataSet
      

  4.   

    //建立DataSet对象中的两个表之间的关联
    DataRelation daR=ds_select.Relations.Add("lkk1",ds_select.Tables["personalInfo"].Columns["id"],ds_select.Tables["Education"].Columns["PersonalInfoid"],false);
      

  5.   

    我能根据子表的Column找到主表的Column吗,通过DataRelation
      

  6.   

    可以,最后参数是false,就不分主,子表
    //建立DataSet对象中的两个表之间的关联
    DataRelation daR=ds_select.Relations.Add("lkk1",ds_select.Tables["Table2"].Columns["A"],ds_select.Tables["Table1"].Columns["B"],false);