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).
Files to Review
- Form1.cs (VB: Form1.vb)
- MasterDetailHelper.cs (VB: MasterDetailHelper.vb)
Documentation
See Also
- Master-Detail Mode - DevExpress WinForms Cheat Sheet
- DevExpress WinForms Troubleshooting - Grid Control
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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;
}
}
}
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();
}
}
}