Create columns from MarkupString

Dear All,

I try to generate a stacked header grid, but I have to create the markup from the code. I did the following:

<div id="grid">
    <SfGrid DataSource="@Orders" AllowSorting = "true" Height=800>
        <GridSortSettings>
            <GridSortColumns>
                <GridSortColumn Field="customer" Direction="SortDirection.Ascending"></GridSortColumn>
            </GridSortColumns>
        </GridSortSettings>
        <GridColumns>
            @myColumns
        </GridColumns>
        <SfSpinner @bind-Visible="@spinnerVisible" CssClass="e-spin-overlay" Label="Loading Data..."></SfSpinner>
    </SfGrid>
</div>

The content of the myColumnsMarkup is the following:

<GridColumn Field="Customer" TextAlign="TextAlign.Left"></GridColumn>
<GridColumn HeaderText="2020">
    <GridColumns>
        <GridColumn Field="Value2020" HeaderText="Value (EUR)" Format="N0" TextAlign="TextAlign.Right" ></GridColumn>
        <GridColumn Field="Profit2020" HeaderText="Profit (EUR)" Format="N0" TextAlign="TextAlign.Right" ></GridColumn>
        <GridColumn Field="ProfitPerc2020" HeaderText="%" Format="N1" TextAlign="TextAlign.Right" > </GridColumn>
    </GridColumns>
</GridColumn>
<GridColumn HeaderText="2021">
    <GridColumns>
        <GridColumn Field="Value2021" HeaderText="Value (EUR)" Format="N0" TextAlign="TextAlign.Right" ></GridColumn>
        <GridColumn Field="Profit2021" HeaderText="Profit (EUR)" Format="N0" TextAlign="TextAlign.Right" ></GridColumn>
        <GridColumn Field="ProfitPerc2021" HeaderText="%" Format="N1" TextAlign="TextAlign.Right" > </GridColumn>
    </GridColumns>
</GridColumn>

However the grid is not appearing, it's palce is empty. What do I wrong?

Strange enough: if I just copy-paste the markup content, the grid appears and all is fine....


9 Replies 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team December 10, 2021 01:27 PM UTC

Hi Peter, 

Greetings from Syncfusion. 

We have validated your query and we suggest you to achieve your requirement by creating a Custom component and render that component in GridColumns tag like below code snippets. Find the below code snippets and sample for your reference. 

[Index.razor] 
 
<SfGrid DataSource="@Orders" AllowResizing="true" AllowPaging="true" Height="315"> 
    <GridColumns> 
        <GridColumnsCustom /> 
    </GridColumns> 
</SfGrid> 
[GridColumnsCustom.razor] 
 
@using Syncfusion.Blazor.Grids  
 
<GridColumn Field="OrderID" HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
<GridColumn HeaderText=" Order Details"> 
     <GridColumns> 
         <GridColumn Field="OrderDate" Width="130" HeaderText="Order Date" Format="d" TextAlign="TextAlign.Right" MinWidth="10"></GridColumn> 
         <GridColumn Field="Freight" Width="135" HeaderText="Freight($)" Format="C2" TextAlign="TextAlign.Right" MinWidth="10" AllowResizing=false></GridColumn> 
     </GridColumns> 
</GridColumn> 
 


Please let us know if you have any concerns. 

Regards, 
Rahul 



PE Peter December 10, 2021 03:30 PM UTC

Dear Rahul,

maybe I was not clear how the myColumnsMarkup is created: it is created on the fly, dynamically, via a c# code, since the columns of the grid are not fixed, they are changing based on user interaction.

What I included is only the result of the c# code, running on a button press. I include a short sample what shows the same problem. Please have a look on the FetchData page, and click on the Filter button.

Please note: the project is for .NET 6/VS 2022


Attachment: BlazorApp1_e72dd35.zip


RN Rahul Narayanasamy Syncfusion Team December 13, 2021 03:15 PM UTC

Hi Peter, 

Thanks for the update. 

We have validated your query with the provided details and you want to create the GridColumns dynamically. Based on your scenario, we suggest you to achieve your requirement by using Columns property. Here, we have created GridColumns dynamically using C# code and set the columns while clicking the button. Find the below code snippets and sample for your reference. 

[Counter.razor] 
<SfButton CssClass="e-outline" IsPrimary="true" @onclick="onFilterClick">Filter</SfButton><br /> 
<SfGrid DataSource="@forecasts" AllowSorting = "true" Height=800 Columns="@Cols"> 
</SfGrid>  
 
@code { 
    private WeatherForecast[]? forecasts; 
    List<GridColumn> Cols = new List<GridColumn>(); //columns bounded to Grid. 
    MarkupString myColumns; 
 
    async Task onFilterClick(Microsoft.AspNetCore.Components.Web.MouseEventArgs args) 
    { 
        forecasts = await ForecastService.GetForecastAsync(DateTime.Now); 
        Cols.Add(new GridColumn() 
        { 
            HeaderText = "2021", 
            Width = "75", 
            Columns = new List<GridColumn>() { 
            new GridColumn() { Field = "Date", HeaderText = "Value (EUR)", Format="N0", TextAlign = TextAlign.Right }, 
            new GridColumn() { Field = "TemperatureC", HeaderText = "Value (EUR)", Format="N0", TextAlign = TextAlign.Right }, 
            new GridColumn() { Field = "TemperatureF", HeaderText = "Value (EUR)", Format="N0", TextAlign = TextAlign.Right }, 
            } 
        }); 
    } 
} 



Please let us know if you have any concerns. 

Regards, 
Rahul 



PE Peter December 13, 2021 05:01 PM UTC

Dear Rahul,


unfortunately I am unable to use your suggestion, since the grid is binded to an ExpandoObject, the columns are coming in an arbitrary order, and in arbitrary number too. The way I can distinguish them is by their name. The example I included is a very simplified version of the column generation.

I still don't understand why the MarkupString is not working as expected? Can you please check the example I included?

As a last resort, should I use RenderFragment instead of MarkupString? Will that work?

Thanks.



RN Rahul Narayanasamy Syncfusion Team December 14, 2021 02:08 PM UTC

Hi Peter, 
 
Thanks for your update. 
 
We are currently checking your reported query at our end and we will update the further details in two business days. Until then we appreciate your patience. 
 
Regards, 
Rahul 



RN Rahul Narayanasamy Syncfusion Team December 16, 2021 10:10 AM UTC

Hi Peter, 

Thanks for your patience. 

We have validated your query and we would like to inform you that it is not possible to render any Custom component or GridColumns using Markup string. Since MarkupString allows you to render raw HTML. So it is was not working as expected.  

Reference:  

If you want to create and render the GridColumns using C# code, then we suggest you to achieve your requirement by using the mentioned way in this forum. In this forum we have created and rendered the Grid using C# code. 


Please let us know if you have any concerns. 

Regards, 
Rahul 


Marked as answer

PE Peter December 16, 2021 11:33 AM UTC

Dear Rahul,

thank you for your time and effort to check my problem. Your guidance is very valuable and without it I would be hooked.

With best regards:

Peter



RN Rahul Narayanasamy Syncfusion Team December 17, 2021 05:47 AM UTC

Hi Peter, 
 
Thanks for the update. 
 
We are happy to hear that the provided solution was helpful. Please get back to us if you need further assistance. 
 
Regards, 
Rahul 



RN Rahul Narayanasamy Syncfusion Team December 17, 2021 08:50 AM UTC

Hi Peter, 

Thanks for the update. 

We are happy to hear that the provided solution was helpful. Please get back to us if you need further assistance. 

Regards, 
Rahul 


Loader.
Up arrow icon