Introduction
This example demonstrates a Windows Forms equivalent to the Microsoft Access one-to-many form by binding a DataSet containing a DataRelation between 2 DataTables to a DataGridView resulting in Parent/Child views.Building the Sample
This sample was built using Visual Studio 2010.Description
In Microsoft Access you can create a form that contains a subform (a one-to-many form) which is useful when working with relational data. For this sample we'll use the Customer Orders scenario giving the user a way to view both Customer and related Order data at the same time by creating a Parent/Child view on a Windows Form using DataGridViews. This is accomplished through the use of a DataRelation between two DataTable objects in a DataSet which are bound to DataGridViews.
Below is a code snippet from Form1.cs which creates the DataSet, DataTables, DataRelation, adds test data, and binds the data to DataGridViews.
The purpose of this sample is to fulfill a code sample request. I hope this helps.
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace FormSubformEquivalent { /// <summary> /// This class demonstrates the use of DataTables and DataRelations, /// creating Master/Child views to mimic a one-to-many form /// (form/subform) in Microsoft Access. This should be considered a /// starting point. /// </summary> public partial class Form1 : Form { private DataTable tblCustomer; private DataTable tblOrder; private DataSet tblDataSet; public Form1() { InitializeComponent(); // Create Tables tblCustomer = new DataTable("tblCustomer"); tblOrder = new DataTable("tblOrder"); // Create DataSet tblDataSet = new DataSet(); // Create Columns and Add to Tables tblCustomer.Columns.Add("ID", typeof(int)); tblCustomer.Columns.Add("CustomerName", typeof(string)); tblOrder.Columns.Add("ID", typeof(int)); tblOrder.Columns.Add("Order", typeof(string)); tblOrder.Columns.Add("CustomerID", typeof(int)); // Add Test Data tblCustomer.Rows.Add(1, "Jane Doe"); tblCustomer.Rows.Add(2, "John Smith"); tblCustomer.Rows.Add(3, "Richard Roe"); tblOrder.Rows.Add(1, "Order1.1", 1); tblOrder.Rows.Add(2, "Order1.2", 1); tblOrder.Rows.Add(3, "Order1.3", 1); tblOrder.Rows.Add(4, "Order2.1", 2); tblOrder.Rows.Add(5, "Order3.1", 3); tblOrder.Rows.Add(6, "Order3.2", 3); // Add Tables to DataSet tblDataSet.Tables.Add(tblCustomer); tblDataSet.Tables.Add(tblOrder); // Create Relation tblDataSet.Relations.Add("CustOrderRelation", tblCustomer.Columns["ID"], tblOrder.Columns["CustomerID"]); BindingSource bsCustomer = new BindingSource(); bsCustomer.DataSource = tblDataSet; bsCustomer.DataMember = "tblCustomer"; BindingSource bsOrder = new BindingSource(); bsOrder.DataSource = bsCustomer; bsOrder.DataMember = "CustOrderRelation"; // Bind Data to DataGridViews dgvCustomer.DataSource = bsCustomer; dgvOrder.DataSource = bsOrder; } } }


No hay comentarios:
Publicar un comentario