Ticket T964862
Visible to All Users

Blazor - How to enable Column Chooser in GridListEditor (the old DxDataGrid)

created 4 years ago

Hi DevExpress!,

We tried to get the ColumnChooser of the Blazor-Grid to work with XAF.

We did the following:

C#
public class BlazorGridFeaturesViewController : ViewController<ListView> { GridListEditor gridListEditor = null; public BlazorGridFeaturesViewController() { } protected override void OnActivated() { base.OnActivated(); gridListEditor = this.View.Editor as GridListEditor; //... } protected override void OnDeactivated() { base.OnDeactivated(); } protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); //... //Todo: if(View.Model.ShowColumnChooser) EnableColumnChooser(); } //... protected void EnableColumnChooser() { var control = gridListEditor.Control as IDxDataGridAdapter; control.DataGridModel.HeaderTemplate = builder => { builder.OpenComponent<DevExpress.Blazor.DxToolbar>(1); //"ChildContent" is a magic string here. m-( //Or as the kids call it these days: this is by convention //See https://docs.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-3.1#child-content builder.AddAttribute(2, "ChildContent", (RenderFragment)((builder2) => { builder2.OpenComponent<DevExpress.Blazor.DxDataGridColumnChooserToolbarItem>(3); builder2.AddAttribute(0, "Alignment", DevExpress.Blazor.ToolbarItemAlignment.Left); builder2.CloseComponent(); })); builder.CloseComponent(); }; } }

The Chooser is shown and works as expected.
BUT - after closing the app and restarting it, all settings are gone (obviously).

So my question is - how can I obtain the settings where the layout for the grid is stored so I can store it somewhere in the UserModel (e.g. through some ModelExtender)? And second: When is a good point in the page-lifecylce/view-lifecycle to save these settings and - more importantly - load them back?

Is this a valid approach anyway from your point of view? If not - what could be a valid approach?

Thanks a lot and best regards!

Answers approved by DevExpress Support

created 4 years ago (modified 3 years ago)

Hello Axel,

Updated:

In v22.1, DxGridListEditor is the main List Editor. The DxGridListEditor supports Column Chooser API. The following article describes how to disable/enable it in your app: IModelListViewBlazor.EnableColumnChooser Property.

Refer to the following topic to learn how to enable DxGridListEditor in older versions: Enable the DxGridListEditor.

Old answer:

To display the Column Chooser in the Blazor Data Grid’s toolbar, follow the steps below:

  1. Add HeaderTemplate to the Data Grid’s markup.
  2. Add the DxDataGridColumnChooserToolbarItem item to the Toolbar component in this template.

In XAF Blazor UI, The HeaderTemplate is available in the DxDataGridModel class. You need to access it as described at Access List View Grid Component Settings Using a Controller. Then, use one of the following ways:

Using a Component Renderer

To create a RenderFragment, add a Razor component to your Blazor module project and create a static method that will return the component's RenderFragment. We demonstrated this idea in the following help topic: Component Renderer. Here is an example:

DataGridHeader.razor

C#
@using DevExpress.Blazor <DxToolbar> <DxDataGridColumnChooserToolbarItem Alignment="ToolbarItemAlignment.Right" /> </DxToolbar> @code { public static RenderFragment Create() => @<DataGridHeader />; }

MyViewController.cs

C#
using DevExpress.ExpressApp; using DevExpress.ExpressApp.Blazor.Editors.Grid; namespace MySolutionName.Module.Blazor.Controllers { public class MyViewController : ViewController<ListView> { protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); if (View.Editor is GridListEditor gridListEditor) { IDxDataGridAdapter dataGridAdapter = gridListEditor.GetDataGridAdapter(); dataGridAdapter.DataGridModel.HeaderTemplate = DataGridHeader.Create(); } } } }

Using a RenderTreeBuilder

Here is an example:

MyViewController.cs

C#
using DevExpress.Blazor; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Blazor.Editors.Grid; using Microsoft.AspNetCore.Components; namespace MySolution.Module.Blazor.Controllers { public class MyViewController : ViewController<ListView> { protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); if(View.Editor is GridListEditor gridListEditor) { IDxDataGridAdapter dataGridAdapter = gridListEditor.GetDataGridAdapter(); dataGridAdapter.DataGridModel.HeaderTemplate = builder => { builder.OpenComponent<DxToolbar>(1); builder.AddAttribute(2, "ChildContent", (RenderFragment)(childBuilder => { childBuilder.OpenComponent<DxDataGridColumnChooserToolbarItem>(3); childBuilder.AddAttribute(0, "Alignment", ToolbarItemAlignment.Left); childBuilder.CloseComponent(); })); builder.CloseComponent(); }; } } } }

Currently, synchronization between grid columns and the ListView model is not fully implemented. This is what we are going to add in the near future. Since we did not work on this task yet, we cannot suggest any solution or a workaround. The DxDataGrid control has its own mechanism for saving and restoring its layout: DxDataGrid<T>.LayoutChanged Event. However, it will likely contradict with our model synchronizers. So, I recommend that you wait until we implement this functionality on our side.

    Show previous comments (11)
    Anatol (DevExpress) 3 years ago

      Hello Peter,

      Unfortunately, this functionality is not available in v21.2. The Column Chooser can be invoked using an Action only in v22.1+.

        Hi Anatol, I have a similar issue, I want to disable the column chooser which is appearing by default. I'm using devExpress version 22.1.4. I know I can enable this with IModelOptionsBlazor.ListViewEnableColumnChooser option as mentioned here https://docs.devexpress.com/eXpressAppFramework/DevExpress.ExpressApp.Blazor.SystemModule.IModelListViewBlazor.EnableColumnChooser?p=net6&utm_source=SupportCenter&utm_medium=website&utm_campaign=docs-feedback&utm_content=T964862 but I don't know how to use it. It will be really helpful if you can provide a sample controller code utilizing this option or using any other way.

        Thank you

        Dennis Garavsky (DevExpress) 3 years ago

          @Ashutosh Rai: I created a separate ticket on your behalf: T1115606: Blazor - How to disable Column Chooser and archived the current ticket (to avoid unnecessary notifications for other trackers).

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.