DataRelation myDataRelation;
myDataRelation = new DataRelation("CustOrd", ds.Tables["Customers"].Columns["CustomerID"], ds.Tables["Orders"].Columns["CustomerID"]);
// Add the relation to the DataSet.
ds.Relations.Add(myDataRelation);
GridOrders.SetDataBinding(ds,"Customers");
GridDetails.SetDataBinding(ds,"Customers.CustOrd");

解决方案 »

  1.   

    可以在dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)的事件里加入读取子表记录的代码,并将读取出来的DataTable赋值给DataGrid2
    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    {
    if(dataGrid1.VisibleRowCount == 0)return;
             DataSet ds2=New DataSet();
             //根据ds1.Tables[0].Rows[dataGrid1.CurrentRowIndex]["主键"]得到ds2
    dataGrid2.DataSource=ds2.Tables[0];
    }