5th March 2015

DevExpress – Hide Nested List View Columns Based on Parent

DevExpress – Hide Nested List View Columns Based on Parent

In a recent project we had the need to hide certain fields within a nested list view based on certain values set against the parent. We had never done this before in a list view using the DevExpress framework and presumed that we would be able to use the Conditional Appearance module to hide fields in nested list views like we previously did within detail views to hide fields based on the current items value, however after reading the DevExpress documentation we released that this was not possible using the Conditional Appearance module and that we would have to hide the columns within the nested list view from within a custom view controller.

The example code below demonstrates how to hide certain fields within a nested list view based on values set within the parent object. The view controller can also be used to change the formation of the columns within the nested list view based on the parent objects values by changing the VisibleIndex property of the GridColumn.

The view controller must be implemented within the Module.Win project.

using System.Linq;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Win.Editors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
/// <summary>
/// Win Nested List View Controller
/// </summary>
public partial class WinNestedListViewController : ViewController

/// <summary> /// Initializes a new instance of the <see cref="WinNestedListViewController"/> class. /// </summary> public WinNestedListViewController()

this.InitializeComponent(); this.RegisterActions(this.components); this.TargetObjectType = typeof(Object); this.TargetViewType = ViewType.ListView;

/// <summary> /// Called when [view controls created]. /// </summary> protected override void OnViewControlsCreated()

if (View.Id == "NestedListViewIdHere")

Object currentObject = (Object)((object[])((DetailView)this.ObjectSpace.Owner).SelectedObjects).FirstOrDefault(); GridListEditor listEditor = ((ListView)View).Editor as GridListEditor; if (listEditor != null && currentObject != null)

GridControl gridControl = (GridControl)View.Control; GridView gridView = (GridView)gridControl.FocusedView; if (object.Value = 1)

GridColumn field = gridView.Columns["FieldOne"]; field.Visible = true; field.VisibleIndex = 1;


GridColumn field = gridView.Columns["FieldOne"]; field.Visible = false; field.VisibleIndex = -1;

Fill in this quick form and discover your digital future
Choose your interests:

Where to find us

We'd love to welcome you into our office! We're only 20 miles north of Peterborough, conveniently just off the A16.

Carver House
Apex Court, Elsoms Way
PE11 3UL