您好,请参考如下代码
- private void Form1_Load(object sender, EventArgs e)
- {
-
- this.composerTableAdapter.Fill(this.c1NWindDataSet.Composer);
- // Create the DataSet and DataTable
- string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
- System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
- @"ComponentOne Samples\Common\C1NWind.mdb") + ";";
- OleDbConnection conn = new OleDbConnection(connectionString);
- OleDbDataAdapter adp_Composer = new OleDbDataAdapter("Select Last, * from Composer", conn);
- OleDbDataAdapter adp_Opus = new OleDbDataAdapter("Select * from Opus", conn);
- DataSet data = new DataSet();
- DataTable table_Composer = new DataTable();
- DataTable table_Opus = new DataTable();
- data.Tables.Add(table_Composer);
- data.Tables.Add(table_Opus);
- adp_Composer.Fill(table_Composer);
- adp_Opus.Fill(table_Opus);
- //Create relation between tables
- data.Relations.Add("Composer_Sale", data.Tables[table_Composer.TableName].Columns["Last"],
- data.Tables[table_Opus.TableName].Columns["Last"]);
- //Bind with the dataset
- c1TrueDBGrid1.DataSource = data;//MasterGrid
- c1TrueDBGrid2.DataSource = data;// ChildGrid
- c1TrueDBGrid1.DataMember = table_Composer.TableName;
- //Provide master grid data member and data relation name to the child grid data member
- c1TrueDBGrid2.DataMember = c1TrueDBGrid1.DataMember + ".Composer_Sale";
- //Set the Child grid
- c1TrueDBGrid1.ChildGrid = c1TrueDBGrid2;
- }
复制代码 |