Example E2428
Visible to All Users

WinForms Data Grid - Use the Layout View as a master View in master-detail mode

This example shows how to:

  • Bind the Grid Control to master-detail data (Order-Details).
  • Use the LayoutView as a master view.
  • Allow the user to select the type of detail view (Grid, Card, or Layout View).

WinForms Data Grid - Use the Layout or Card View as a master View in master-detail mode

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

WindowsApplication3/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid.ViewInfo; namespace WindowsApplication3 { public partial class Form1: Form { public Form1() { InitializeComponent(); } MasterDetailHelper helper; private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'nwindDataSet.Orders' table. You can move, or remove it, as needed. this.ordersTableAdapter.Fill(this.nwindDataSet.Orders); // TODO: This line of code loads data into the 'nwindDataSet.Order_Details' table. You can move, or remove it, as needed. this.order_DetailsTableAdapter.Fill(this.nwindDataSet.Order_Details); gridControl1.ForceInitialize(); helper = new MasterDetailHelper(layoutView1, ViewType.Grid); helper.CreateDetail(); radioGroup1.Properties.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem(ViewType.Grid, "Grid")); radioGroup1.Properties.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem(ViewType.Layout, "Layout")); radioGroup1.Properties.Items.Add(new DevExpress.XtraEditors.Controls.RadioGroupItem(ViewType.Card, "Card")); radioGroup1.SelectedIndex = 0; } private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e) { helper.ViewType = (ViewType)radioGroup1.EditValue; } } }
WindowsApplication3/MasterDetailHelper.cs(vb)
C#
using System; using System.Collections.Generic; using System.Text; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Columns; using DevExpress.XtraEditors.Repository; using DevExpress.XtraEditors; using System.Collections; using DevExpress.XtraGrid.Views.Card; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Layout; using DevExpress.XtraGrid.Views.Base.ViewInfo; using System.Drawing; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.Utils.Win; using DevExpress.XtraEditors.Popup; namespace WindowsApplication3 { public enum ViewType { Grid, Layout, Card } public class MasterDetailHelper { ColumnView view, detailView; ViewType viewType; GridControl detailGrid; public MasterDetailHelper(ColumnView view, ViewType viewType) { this.view = view; this.viewType = viewType; } public GridControl DetailGrid { get { if(detailGrid == null) { detailGrid = CreateGrid(); detailGrid.MainView = DetailView; } return detailGrid; } } public ColumnView DetailView { get { if(detailView == null) detailView = CreateView(); return detailView; } } public ViewType ViewType { get { return viewType; } set { if(viewType != value) { viewType = value; detailView = null; DetailGrid.MainView = DetailView; } } } public void CreateDetail() { SetColumnEdit(CreateColumn()); } private GridControl CreateGrid() { GridControl grid = new GridControl(); grid.Dock = System.Windows.Forms.DockStyle.Fill; return grid; } private ColumnView CreateView() { if(viewType == ViewType.Card) detailView = new CardView(DetailGrid); else if(viewType == ViewType.Grid) { detailView = new GridView(DetailGrid); ((GridView)detailView).OptionsView.ShowGroupPanel = false; } else detailView = new LayoutView(DetailGrid); return detailView; } private GridColumn CreateColumn() { GridColumn column = view.Columns.AddField("Detail"); column.Visible = true; column.VisibleIndex = view.Columns.Count; return column; } private void SetColumnEdit(GridColumn column) { column.ColumnEdit = CreateItem(); } private RepositoryItemPopupContainerEdit CreateItem() { RepositoryItemPopupContainerEdit item = new RepositoryItemPopupContainerEdit(); item.PopupControl = CreatePopupControl(); item.ShowPopupCloseButton = item.ShowPopupShadow = false; item.PopupSizeable = false; item.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.HideTextEditor; item.QueryPopUp += OnPopup; item.CloseUp += OnCloseUp; view.GridControl.RepositoryItems.Add(item); return item; } private PopupContainerControl CreatePopupControl() { PopupContainerControl popupControl = new PopupContainerControl(); popupControl.Controls.Add(DetailGrid); return popupControl; } void OnPopup(object sender, EventArgs e) { DetailGrid.BeginUpdate(); try { DetailGrid.DataSource = null; DetailGrid.DataSource = GetDetailData(); } finally { DetailGrid.EndUpdate(); } PopupContainerEdit edit = sender as PopupContainerEdit; if(edit.Properties.PopupControl.Parent == null) edit.Properties.PopupControl.Parent = edit.FindForm(); DetailGrid.ForceInitialize(); edit.Properties.PopupFormSize = CalcDetailViewSize(); } private IList GetDetailData() { return view.DataController.GetDetailList(view.FocusedRowHandle, 0); } private System.Drawing.Size CalcDetailViewSize() { ColumnViewInfo viewInfo = DetailView.GetViewInfo() as ColumnViewInfo; Rectangle rect = new Rectangle(0, 0, view.GridControl.ClientSize.Width, DetailView.DetailHeight); rect.Height = viewInfo.CalcRealViewHeight(rect, true) + 5; return rect.Size; } void OnCloseUp(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e) { DetailView.PostEditor(); detailView.UpdateCurrentRow(); } } }

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.