Ticket T961969
Visible to All Users

Xaf Blazor - How to use a custom cell template (HTML markup) in Grid List Editor

created 4 years ago

Hi,
I saw that Blazor grid control supports Row and column templates.
But i don't know how to use this in a xaf blazor app.
Can you put a simple example?

Thanks
Akın GÜNEŞ

Comments (2)
Anatol (DevExpress) 4 years ago

    Hello Akın,

    The following section describes templates supported by the DxDataGrid control: DxDataGrid Class - Templates.
    XAF Blazor UI supports only HeaderTemplate and Column DisplayTemplate. Please describe your ultimate goal in greater detail. We will see if it can be implemented with the current capabilities.

      Hello Anatol,
      Thank you for your reply. I already saw the documentation DxDataGrid Class - Templates. The think i don't know how to use this in an Xaf Blazor UI app since the behavior is implemented in razor syntax in the examples.
      if you can provide a small sample project it will be very helpfull for me.

      We are in a process converting existing xaf web app to xaf blazor ui. if it helps i can send some real template examples from our app that we should performs same customization in blazor ui.

      Thnaks

      Answers approved by DevExpress Support

      created 4 years ago (modified 2 years ago)

      Hello Akin,

      To implement a custom cell template in an XAF Blazor project, create a new Razor Component:

      CellDisplayTemplate.razor

      Razor
      @using DevExpress.Blazor @code { public static RenderFragment<GridDataColumnCellDisplayTemplateContext> Create() => (item) => { GridDataColumnCellDisplayTemplateContext c = item as GridDataColumnCellDisplayTemplateContext; return@<div>@((MarkupString)c.DisplayText)</div>; }; }

      Then, add a controller that passes this component to the corresponding template:

      CustomizeCellTemplateController.cs

      C#
      using DevExpress.ExpressApp; using DevExpress.ExpressApp.Blazor.Editors; namespace MainDemo.Blazor.Server; public class CustomizeCellTemplateController : ObjectViewController<ListView, Employee> { protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); var gridListEditor = (DxGridListEditor)View.Editor; var dataGridAdapter = gridListEditor.GetGridAdapter(); foreach (var columnModel in dataGridAdapter.GridDataColumnModels) { if (columnModel.FieldName == "FirstName") { columnModel.CellDisplayTemplate = CellDisplayTemplate.Create(); } } } }

      In addition, it is possible to render a component for a grid template directly in a ViewController:

      C#
      using DevExpress.Blazor; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Blazor.Editors; using MainDemo.Module.BusinessObjects; namespace MainDemo.Blazor.Server; public class CustomizeCellTemplateController : ObjectViewController<ListView, Employee> { protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); var gridListEditor = (DxGridListEditor)View.Editor; var dataGridAdapter = gridListEditor.GetGridAdapter(); foreach (var columnModel in dataGridAdapter.GridDataColumnModels) { if (columnModel.FieldName == "FirstName") { columnModel.CellDisplayTemplate = (GridDataColumnCellDisplayTemplateContext context) => builder => { builder.AddMarkupContent(0, context.DisplayText); }; } } } }

      You can see more examples in the following tickets:
      How to show a translated enum value in a custom CellDisplayTemplate
      Blazor - How to create an inline listview action that is shown in each row's cell


      Examples for DxDataGrid (GridListEditor):

      CellDisplayTemplate.razor

      Razor
      @using MainDemo.Module.BusinessObjects @code { public static RenderFragment<object> Create() => (item) => { Contact contact = item as Contact; return@<div>@contact.FirstName</div>; }; }

      CustomizeCellTemplateController.cs

      C#
      using DevExpress.ExpressApp; using DevExpress.ExpressApp.Blazor.Editors.Grid; using DevExpress.ExpressApp.Blazor.Editors.Grid.Models; using MainDemo.Module.BusinessObjects; namespace MainDemo.Module.Blazor { public class CustomizeCellTemplateController : ObjectViewController<ListView, Contact> { protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); GridListEditor gridListEditor = (GridListEditor)View.Editor; IDxDataGridAdapter dataGridAdapter = gridListEditor.GetDataGridAdapter(); foreach (DxDataGridColumnModel columnModel in dataGridAdapter.DataGridColumnModels) { if (columnModel.Field == "FirstName") { columnModel.DisplayTemplate = CellDisplayTemplate.Create(); } } } } }
        Comments (2)

          Sorry for my late answer.

          I tried your comment but is not possible to do this. I attached a project. When you run the project in "Ultimos Valores" option you can press the button in action field. I would like to get the change event when you press the button switch but I have an exception.

          Thanks a lot.

          Anatol (DevExpress) 3 years ago

            Hello,

            I can see that your Razor Component is incorrectly bound to received parameters. I created a separate ticket to discuss this issue: Blazor - How to show a toggle switch in ListView cells.

            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.