Trying to apply Column Formats to Dynamically created Columns

Hello,


I have successfully created a dynamic grid to view data from different CSV files. I am now trying to apply C2 Format to certain columns, but the formatting is not being applied. This is the code I'm using:

        foreach(var cf in Grid1.Columns)
        {
            var htxt = cf.HeaderText;
            if (DateColumns.Contains(htxt))
                cf.Format = "d";
            else if (CurrencyColumns.Contains(htxt))
                cf.Format = "C2";
        };

This is code is run within OnInitializedAsync, after data from the CSV has been loaded.

The Grid displays correctly, except for the formats not being applied.

The latest thing I've tried is: 

await Grid1.RefreshColumnsAsync();
StateHasChanged();

But still nothing.


It feels I'm missing something obvious. Can you please help?

Regards


Furio Filoseta



1 Reply

NP Naveen Palanivel Syncfusion Team June 30, 2023 01:54 PM UTC

Hi Furio,


We reviewed your query and discovered that you want to apply format to the dynamically created column . Based on your requirement, we created an simple sample and apply  format for the double and datetime columns. Please see the code snippet and sample for further information.


<GridColumns>

        @foreach (var prop in typeof(Order).GetProperties())

        {

            if(prop.PropertyType == typeof(double))

            {

                <GridColumn Field="@prop.Name" Format="C2" AllowEditing="@prop.CanWrite"></GridColumn>

            }

            else if(prop.PropertyType == typeof(DateTime))

            {

                <GridColumn Field="@prop.Name" Format="MM/dd/yyyy" AllowEditing="@prop.CanWrite"></GridColumn>

            }

            else

            {

                <GridColumn Field="@prop.Name" IsPrimaryKey="@(prop.Name == "OrderID")" AllowEditing="@prop.CanWrite"></GridColumn>

            }

 

          

        }

    </GridColumns>



Please let us know if you have any concerns.


Regards,

Naveen Palanivel


Attachment: Blazor_Format_f25b270b.zip

Loader.
Up arrow icon