日期:2014-05-19  浏览次数:20989 次

主从表关联?在线等
DataRelation   myDataRelation   =   new   DataRelation( "business_r ",   dataSet1.Tables[ "business_h "].Columns[ "business_id "],   dataSet1.Tables[ "business_d "].Columns[ "business_id "]);
                        dataSet1.Relations.Add(myDataRelation);
                        bUSINESS_HDataGridView.DataSource   =   dataSet1.Tables[ "business_h "];
                        bUSINESS_DDataGridView.DataSource   =   dataSet1.Tables[ "business_d "];

我用这句代码来实现主从表的关联,可是没有作用,应怎么去关联主从表?

------解决方案--------------------
你关联它们要实现什么,
如果是win Form,试一下:bUSINESS_HDataGridView.DataSource = dataSet1
------解决方案--------------------
OleDbConnection con = new OleDbConnection(@ "../gb.mdb ");
OleDbDataAdapter da = new OleDbDataAdapter( "select * from guestbook ",con);
OleDbDataAdapter da2 = new OleDbDataAdapter( "select * from reply ",con);

DataSet ds = new DataSet();
da.Fill(ds, "guestbook ");
da2.Fill(ds, "reply ");

ds.Relations.Add( "relation ",ds.Tables[0].Columns[ "id "],ds.Tables[1].Columns[ "id "]);

parent.DataSource = ds.Tables[0].DefaultView;
parent.DataBind();

con.Close();

写ItemDataBound 事件,这个很关键.

DataRowView dv = e.Item.DataItem as DataRowView;

child = e.Item.FindControl( "child ") as Repeater;
child.DataSource = dv.CreateChildView( "relation ");
child.DataBind();
------解决方案--------------------
DataRelation myDataRelation = new DataRelation( "business_r ", dataSet1.Tables[ "business_h "].Columns[ "business_id "], dataSet1.Tables[ "business_d "].Columns[ "business_id "]);
dataSet1.Relations.Add(myDataRelation);
bUSINESS_HDataGridView.DataSource = dataSet1.Tables[ "business_h "];
bUSINESS_DDataGridView.DataSource = dataSet1.Relations[myDataRelation];
------解决方案--------------------
bUSINESS_DDataGridView.DataSource 的DataSource有问题,它不是绑在Dataset1中了,它应绑定在 Relation上.....

找资料看一下