Blogs

Blogs

DevExpress – Hide Nested List View Columns Based on Parent

5th March 2015

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;
                }
                else
                {
                    GridColumn field = gridView.Columns["FieldOne"];
                    field.Visible = false;
                    field.VisibleIndex = -1;
                }
            }
        }
    }
}
This website uses cookies to provide the best possible experience. By continuing to use our website you are agreeing to our use of cookies.