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.
C#
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
No comments:
Post a Comment