效果如图:
Problem
You need to bind both a parent table and child table within a DataSet to a pair of DataGridView controls so that the child data is displayed when a parent is selected.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace MasterDetailWindowsFormDataGrid
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Configure the layout properties of the header grid
headerDataGridView.Anchor = AnchorStyles.Left | AnchorStyles.Right |
AnchorStyles.Top;
headerDataGridView.ReadOnly = true;
headerDataGridView.AllowUserToAddRows = false;
headerDataGridView.AllowUserToDeleteRows = false;
headerDataGridView.MultiSelect = false;
headerDataGridView.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells;
headerDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
// Configure layout properties of the detail grid
detailDataGridView.Anchor = AnchorStyles.Left | AnchorStyles.Right |
AnchorStyles.Top;
detailDataGridView.ReadOnly = true;
detailDataGridView.AllowUserToAddRows = false;
detailDataGridView.AllowUserToDeleteRows = false;
detailDataGridView.MultiSelect = false;
detailDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
detailDataGridView.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells;
this.Width = 600;
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
// Retrieve a DataSet containing sales order header and detail data in
// two related DataTable objects
DataSet ds = LoadData();
// Create and assign a BindingSource for each data grid
BindingSource headerBindingSource = new BindingSource();
headerDataGridView.DataSource = headerBindingSource;
BindingSource detailBindingSource = new BindingSource();
detailDataGridView.DataSource = detailBindingSource;
// Set the data source of the header grid to the SalesOrderHeader table
headerBindingSource.DataSource = ds;
headerBindingSource.DataMember = "SalesOrderHeader";
// Set the data source of the detail grid to the related SalesOrderDetail
// records
detailBindingSource.DataSource = headerBindingSource;
detailBindingSource.DataMember = "FK_SalesOrderDetail_SalesOrderHeader";
}
protected DataSet LoadData()
{
string sqlConnectString = @"Data Source=(local);
Integrated security=SSPI;Initial Catalog=AdventureWorks;";
string sqlSelect =
@"SELECT SalesOrderID, OrderDate, SalesOrderNumber, TotalDue
FROM Sales.SalesOrderHeader
WHERE SalesOrderID BETWEEN 43660 AND 43669;
SELECT SalesOrderID, SalesOrderDetailID, OrderQty, ProductID, LineTotal
FROM Sales.SalesOrderDetail
WHERE SalesOrderID BETWEEN 43660 AND 43669;";
// Fill and return a DataSet containing sales order header and sales order
// detail DataTable objects that are related on the SalesOrderID field
SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString);
da.TableMappings.Add("Table", "SalesOrderHeader");
da.TableMappings.Add("Table1", "SalesOrderDetail");
DataSet ds = new DataSet();
da.Fill(ds);
// Add a relation between parent and child table.
ds.Relations.Add("FK_SalesOrderDetail_SalesOrderHeader",
ds.Tables["SalesOrderHeader"].Columns["SalesOrderID"],
ds.Tables["SalesOrderDetail"].Columns["SalesOrderID"]);
return ds;
}
}
}
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace MasterDetailWindowsFormDataGrid
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Configure the layout properties of the header grid
headerDataGridView.Anchor = AnchorStyles.Left | AnchorStyles.Right |
AnchorStyles.Top;
headerDataGridView.ReadOnly = true;
headerDataGridView.AllowUserToAddRows = false;
headerDataGridView.AllowUserToDeleteRows = false;
headerDataGridView.MultiSelect = false;
headerDataGridView.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells;
headerDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
// Configure layout properties of the detail grid
detailDataGridView.Anchor = AnchorStyles.Left | AnchorStyles.Right |
AnchorStyles.Top;
detailDataGridView.ReadOnly = true;
detailDataGridView.AllowUserToAddRows = false;
detailDataGridView.AllowUserToDeleteRows = false;
detailDataGridView.MultiSelect = false;
detailDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
detailDataGridView.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells;
this.Width = 600;
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
// Retrieve a DataSet containing sales order header and detail data in
// two related DataTable objects
DataSet ds = LoadData();
// Create and assign a BindingSource for each data grid
BindingSource headerBindingSource = new BindingSource();
headerDataGridView.DataSource = headerBindingSource;
BindingSource detailBindingSource = new BindingSource();
detailDataGridView.DataSource = detailBindingSource;
// Set the data source of the header grid to the SalesOrderHeader table
headerBindingSource.DataSource = ds;
headerBindingSource.DataMember = "SalesOrderHeader";
// Set the data source of the detail grid to the related SalesOrderDetail
// records
detailBindingSource.DataSource = headerBindingSource;
detailBindingSource.DataMember = "FK_SalesOrderDetail_SalesOrderHeader";
}
protected DataSet LoadData()
{
string sqlConnectString = @"Data Source=(local);
Integrated security=SSPI;Initial Catalog=AdventureWorks;";
string sqlSelect =
@"SELECT SalesOrderID, OrderDate, SalesOrderNumber, TotalDue
FROM Sales.SalesOrderHeader
WHERE SalesOrderID BETWEEN 43660 AND 43669;
SELECT SalesOrderID, SalesOrderDetailID, OrderQty, ProductID, LineTotal
FROM Sales.SalesOrderDetail
WHERE SalesOrderID BETWEEN 43660 AND 43669;";
// Fill and return a DataSet containing sales order header and sales order
// detail DataTable objects that are related on the SalesOrderID field
SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString);
da.TableMappings.Add("Table", "SalesOrderHeader");
da.TableMappings.Add("Table1", "SalesOrderDetail");
DataSet ds = new DataSet();
da.Fill(ds);
// Add a relation between parent and child table.
ds.Relations.Add("FK_SalesOrderDetail_SalesOrderHeader",
ds.Tables["SalesOrderHeader"].Columns["SalesOrderID"],
ds.Tables["SalesOrderDetail"].Columns["SalesOrderID"]);
return ds;
}
}
}