Skip to main content

How to: Replace a View Used to Represent a Specific Master-Detail Relationship

  • 2 minutes to read

Assume that a grid control displays a master-detail relationship between two tables. The relationship is named “Orders”. The following example shows how to replace the existing View representing the “Orders” relationship with a new banded View.

First a node within the GridControl.LevelTree representing the “Orders” relationship is located. The existing View is disposed of and a new banded View is then assigned to this relationship.

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.BandedGrid;

// Collapse all the details opened for the master rows in the main view.
(gridControl1.MainView as GridView).CollapseAllDetails();

// Get the node at the first nesting level that stores a view for the "Orders" relation.
GridLevelNode node = gridControl1.LevelTree.Nodes["Orders"];
if(node == null) return;
// The old view which represents the "Orders" relation.
BaseView oldView = node.LevelTemplate;         
// Dispose of this view.
oldView.Dispose();

// Create a new view.
BandedGridView bandedView = new BandedGridView(gridControl1);            
// Associate this view with the "Orders" relation.
node.LevelTemplate = bandedView;

// Customize the new view.
GridBand band = bandedView.Bands.Add("Orders");
BandedGridColumn column = (BandedGridColumn)bandedView.Columns.Add("ID");
column.OwnerBand = band;
column.Visible = true;

column = (BandedGridColumn)bandedView.Columns.AddField("ProductID");
column.OwnerBand = band;
column.Visible = true;