假设网格控件显示了在两个表之间的主/从关系。 此关系被命名为“Orders”。 下面的示例展示了如何把现有的呈现“Orders”关系的视图,替换为一个新的带区视图。

首先,在 GridControl.LevelTree 中查找呈现“Orders”关系的节点。 然后,现有的视图被销毁,并且把一个新的带区视图指派到此关系。

C#CopyCode image复制代码
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;

Visual BasicCopyCode image复制代码
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