Example E883
Visible to All Users

WinForms Data Grid - How to display master-detail tables in separate grid controls

The data source in this example is a DataSet object that contains master and detail tables linked by the CustomersPersons relationship.
The example shows how to display these tables in separate Grid Controls.
The first grid is bound to a master table. The second grid displays details for the master row selected in the first grid.

To link two grid controls, the second grid is bound to a BindingSource component. This component is initialized as follows:

  • The BindingSource.DataSource property is set to the first grid's data source.
  • The BindingSource.DataMember property is set to CustomersPersons (the name of the relationship between the master and detail tables).

Files to Review

Documentation

See Also

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Q205299/Form1.cs(vb)
C#
using System.Data; using System.Windows.Forms; namespace Q205299 { public partial class Form1 : Form { public Form1() { InitializeComponent(); DataSet dataSet11 = new DataSet(); dataSet11.Tables.Add(GetCustomerDataTable()); dataSet11.Tables.Add(GetPersonDataTable()); DataColumn keyColumn = dataSet11.Tables["Customers"].Columns["ID"]; DataColumn foreignKeyColumn = dataSet11.Tables["Persons"].Columns["ID"]; dataSet11.Relations.Add("CustomersPersons", keyColumn, foreignKeyColumn); bindingSource1.DataSource = dataSet11; bindingSource1.DataMember = "Customers"; bindingSource2.DataSource = bindingSource1; bindingSource2.DataMember = "CustomersPersons"; gridControl1.DataSource = bindingSource1; gridControl2.DataSource = bindingSource2; } DataTable GetCustomerDataTable() { DataTable table = new DataTable(); table.TableName = "Customers"; table.Columns.Add(new DataColumn("Items", typeof(string))); table.Columns.Add(new DataColumn("Money", typeof(double))); table.Columns.Add(new DataColumn("ID", typeof(int))); for (int i = 0; i < 10; i++) table.Rows.Add("Product " + i, 3000 + i * 298.55M, i); return table; } DataTable GetPersonDataTable() { DataTable table = new DataTable(); table.TableName = "Persons"; table.Columns.Add(new DataColumn("FirstName", typeof(string))); table.Columns.Add(new DataColumn("SecondName", typeof(string))); table.Columns.Add(new DataColumn("Age", typeof(int))); table.Columns.Add(new DataColumn("ID", typeof(int))); for (int i = 0; i < 50; i++) { string name = "Adam"; string secondName = "Smith"; if (i % 2 == 0) { name = "Ben"; secondName = "Black"; } table.Rows.Add(name, secondName, 20 + i / 2, i % 10); } return table; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.