Example E3858
Visible to All Users

WinForms Data Grid - Visualize master-detail data (runtime)

This example shows how to bind the WinForms Grid Control to the NorthWind database and implement master-detail relationships (Category-Product).

The GridView displays data from the Categories data table. The CardView displays data from the Products data table. These data tables are linked by a master-detail relationship that is set up in code.

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

Form1.cs(vb)
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; using System.Data.OleDb; using DevExpress.XtraGrid.Views.Card; using DevExpress.XtraEditors.Repository; namespace Grid_Runtime_MasterDetail_Mode { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Define a connection to the database OleDbConnection connection = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = ..\\..\\Data\\nwind.mdb"); //Create data adapters for retrieving data from the tables OleDbDataAdapter AdapterCategories = new OleDbDataAdapter( "SELECT CategoryID, CategoryName, Picture FROM Categories", connection); OleDbDataAdapter AdapterProducts = new OleDbDataAdapter( "SELECT CategoryID, ProductID, ProductName, UnitPrice FROM Products", connection); DataSet dataSet11 = new DataSet(); //Create DataTable objects for representing database's tables AdapterCategories.Fill(dataSet11, "Categories"); AdapterProducts.Fill(dataSet11, "Products"); //Set up a master-detail relationship between the DataTables DataColumn keyColumn = dataSet11.Tables["Categories"].Columns["CategoryID"]; DataColumn foreignKeyColumn = dataSet11.Tables["Products"].Columns["CategoryID"]; dataSet11.Relations.Add("CategoriesProducts", keyColumn, foreignKeyColumn); //Bind the grid control to the data source gridControl1.DataSource = dataSet11.Tables["Categories"]; gridControl1.ForceInitialize(); //Assign a CardView to the relationship CardView cardView1 = new CardView(gridControl1); gridControl1.LevelTree.Nodes.Add("CategoriesProducts", cardView1); //Specify text to be displayed within detail tabs. cardView1.ViewCaption = "Category Products"; //Hide the CategoryID column for the master View gridView1.Columns["CategoryID"].VisibleIndex = -1; //Present data in the Picture column as Images RepositoryItemPictureEdit riPictureEdit = gridControl1.RepositoryItems.Add("PictureEdit") as RepositoryItemPictureEdit; gridView1.Columns["Picture"].ColumnEdit = riPictureEdit; //Stretch images within cells. riPictureEdit.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch; gridView1.Columns["Picture"].OptionsColumn.FixedWidth = true; //Change Picture column's width gridView1.Columns["Picture"].Width = 180; //Change row height in the master View gridView1.RowHeight = 50; //Create columns for the detail pattern View cardView1.PopulateColumns(dataSet11.Tables["Products"]); //Hide the CategoryID column for the detail View cardView1.Columns["CategoryID"].VisibleIndex = -1; //Format UnitPrice column values as currency cardView1.Columns["UnitPrice"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric; cardView1.Columns["UnitPrice"].DisplayFormat.FormatString = "c2"; } } }

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.