Back to Devexpress

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

windowsforms-3058-controls-and-libraries-data-grid-examples-master-detail-how-to-replace-a-view-used-to-represent-a-specific-master-detail-relationship.md

latest2.8 KB
Original Source

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

  • Nov 13, 2018
  • 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.

csharp
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;
vb
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.BandedGrid

' Collapse all the details opened for the master rows in the main view.
CType(GridControl1.MainView, GridView).CollapseAllDetails()

' Get the node at the first nesting level that stores a view for the "Orders" relation.
Dim node As GridLevelNode = GridControl1.LevelTree.Nodes("Orders")
If node Is Nothing Then Return
' The old view which represents the "Orders" relation.
Dim oldView As BaseView = node.LevelTemplate
' Dispose of this view.
oldView.Dispose()

' Create a new view.
Dim bandedView As BandedGridView = New BandedGridView(GridControl1)
' Associate this view with the "Orders" relation.
node.LevelTemplate = bandedView

' Customize the new view.
Dim band As GridBand = bandedView.Bands.Add("Orders")
Dim column As BandedGridColumn = bandedView.Columns.Add("ID")
column.OwnerBand = band
column.Visible = True

column = bandedView.Columns.AddField("ProductID")
column.OwnerBand = band
column.Visible = True