WinForm 2.0 有代码两个DataGridView实现Master/Details
2007-11-25 21:46
344 查看
private void GetData()
{
try
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample
// database accessible to your system.
String connectionString =
"Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost";
SqlConnection connection = new SqlConnection(connectionString);
// Create a DataSet.
DataSet data = new DataSet();
data.Locale = System.Globalization.CultureInfo.InvariantCulture;
// Add data from the Customers table to the DataSet.
SqlDataAdapter masterDataAdapter = new
SqlDataAdapter("select * from Customers", connection);
masterDataAdapter.Fill(data, "Customers");
// Add data from the Orders table to the DataSet.
SqlDataAdapter detailsDataAdapter = new
SqlDataAdapter("select * from Orders", connection);
detailsDataAdapter.Fill(data, "Orders");
// Establish a relationship between the two tables.
DataRelation relation = new DataRelation("CustomersOrders",
data.Tables["Customers"].Columns["CustomerID"],
data.Tables["Orders"].Columns["CustomerID"]);
data.Relations.Add(relation);
// Bind the master data connector to the Customers table.
this.bindingSource1.DataSource = data;
this.bindingSource1.DataMember = "Customers";
// Bind the details data connector to the master data connector,
// using the DataRelation name to filter the information in the
// details table based on the current row in the master table.
this.bindingSource2.DataSource = bindingSource1;
this.bindingSource2.DataMember = "CustomersOrders";
}
catch (SqlException)
{
MessageBox.Show("To run this example, replace the value of the " +
"connectionString variable with a connection string that is " +
"valid for your system.");
}
}
private void Form1_Load(object sender, EventArgs e)
{
// Bind the DataGridView controls to the BindingSource
// components and load the data from the database.
this.dataGridView1 .DataSource = bindingSource1;
this.dataGridView2.DataSource = bindingSource2;
GetData();
// Resize the master DataGridView columns to fit the newly loaded data.
dataGridView1.AutoResizeColumns();
// Configure the details DataGridView so that its columns automatically
// adjust their widths when the data changes.
dataGridView2.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells;
this.bindingNavigator1.BindingSource = this.bindingSource1;
}
相关文章推荐
- WinForm 2.0 无代码两个DataGridView实现Master/Details
- WinForm 2.0 有代码两个DataGridView实现Master/Details
- WinForm 2.0 无代码实现DataGridView数据和下拉框和文本框的同步
- winForm画面缩放的代码,实现控件缩放,字体缩放,labe右对齐,datagridview字体缩放
- WinForm 2.0 应用一 无代码实现详细信息绑定
- WinForm 2.0 无代码DataGridView绑定
- WinForm 2.0 无代码DataGridView绑定
- WinForm 2.0 应用一 无代码实现详细信息绑定
- 在图像上画线(给出任意两个点)c代码实现
- AjaxPro.NET框架实现服务端即时数据验证(Asp.net 2.0)(示例代码下载)
- 实现WinForm的DataGridView折叠功能(非原创,仅供收藏)
- WinForm下DataGridView导出Excel的实现
- 【转】Winform中DataGridView控件实现拖动行,并在拖动状态中目标行上显示一条红线。
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- 转载:winform的DataGridView中用C#实现按钮列置灰
- winform程序两个窗体间同步数据(一): 静态变量和线程实现
- c# Winform Listview的Details模式时拖动Items排序的实现。
- .NET 2.0 - WinForm Control - DataGridView 编程36计(二)
- .NET 2.0 - WinForm Control - DataGridView 编程36计(二)