不用那么复杂,举NorthWind数据库的Customers和Orders表为例:SqlDataAdapter daa=new SqlDataAdapter("select * from Customers",this.sqlConnection1);
DataSet ds=new DataSet();
daa.Fill(ds,"customers");
daa.SelectCommand.CommandText="select * from Orders";
daa.Fill(ds,"orders");
ds.Relations.Add("customerID_relation",ds.Tables["customers"].Columns["CustomerID"],ds.Tables["orders"].Columns["CustomerID"]);
this.dataGrid1.DataSource=ds.Tables["customers"];
this.dataGrid1.DataMember="customerID_relation";
this.listBox1.DataSource=ds.Tables["customers"];
this.listBox1.DisplayMember="CompanyName";
DataSet ds=new DataSet();
daa.Fill(ds,"customers");
daa.SelectCommand.CommandText="select * from Orders";
daa.Fill(ds,"orders");
ds.Relations.Add("customerID_relation",ds.Tables["customers"].Columns["CustomerID"],ds.Tables["orders"].Columns["CustomerID"]);
this.dataGrid1.DataSource=ds.Tables["customers"];
this.dataGrid1.DataMember="customerID_relation";
this.listBox1.DataSource=ds.Tables["customers"];
this.listBox1.DisplayMember="CompanyName";
开个玩笑 :)
DataSet ds=new DataSet();
daa.Fill(ds,"customers");
daa.SelectCommand.CommandText="select * from Orders";
daa.Fill(ds,"orders");
ds.Relations.Add("customerID_relation",ds.Tables["customers"].Columns["CustomerID"],ds.Tables["orders"].Columns["CustomerID"]);
this.listBox1.DataSource=ds.Tables["customers"];
this.listBox1.DisplayMember="CompanyName";
this.listBox2.DataSource=ds.Tables["Orders"];
this.listBox2.DisplayMember="RequiredDate";
((DataTable)listBox2.DataSource).DefaultView.RowFilter="CustomerID='"+((DataRowView)this.BindingContext[listBox1.DataSource].Current)["CustomerID"].ToString()+"'";另外,在ListBox2的selectedIndexChanged事件中加入下面代码:
((DataTable)listBox2.DataSource).DefaultView.RowFilter="CustomerID='"+((DataRowView)this.BindingContext[listBox1.DataSource].Current)["CustomerID"].ToString()+"'";
listBox1.DisplayMember = "customers.CompanyName";
listBox2.DataSource = ds;
listBox2.DisplayMember = "customers.customerID_relation.RequireDate";
没测试,但想来应该可以.