This is a migrated thread and some comments may be shown as answers.
Grid Column - DateTime Format
5 Answers 210 Views
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 2
Michael asked on 06 Aug 2020, 02:06 PM

Hello community! 

Is there a way to format the DateTime value of our model without using templates? 

 

Thanks in advance for any tip!

<GridColumn Field=@nameof(WillyMachine.DateDebut) Title="Debut" Filterable="true" Editable="false"  Width="100px"/>

5 Answers, 1 is accepted

Sort by
0
Accepted
Marin Bratanov
Telerik team
answered on 06 Aug 2020, 02:43 PM

Hi Michael,

When this feature gets implemented there will be: https://feedback.telerik.com/blazor/1451067-add-attribute-format-for-grid-column-to-apply-c-standard-formats. I've added your Vote to it to raise its priority, and you can click the Follow button for email notifications.

 

Regards,
Marin Bratanov
Progress Telerik

0
Michael
Top achievements
Rank 2
answered on 06 Aug 2020, 05:46 PM
You're the best Marin
0
Zoya
Top achievements
Rank 1
answered on 15 Sep 2020, 01:52 PM

Hi,

 

I have added my Vote also: date formats in the grid would be fine..

I want to try something a little bit different but still using templates.

This code works right:

                        <Telerik.Blazor.Components.GridColumn Field="@nameof(Security.ValidFrom)" Width="100px">
                            <Template>
                                @((context as Security).ValidFrom.ToString("dd.MM.yyyy"))
                            </Template>
                        </Telerik.Blazor.Components.GridColumn>

But I want to have "a dynamic grid" with DataTable Source:

    <GridColumns>
        @foreach (DataColumn col in DataTable.Columns)
        {
            switch (col.DataType.FullName)
            {
                case "System.Int32":
                    <GridColumn Field="@col.ToString()" FieldType="@typeof(decimal)" Width="80px" />
                    break;
                case "System.DateTime":
                    <GridColumn Field="@col.ToString()" FieldType="@typeof(DateTime)" Width="100px" />
                    break;
                case "System.Boolean":
                    <GridColumn Field="@col.ToString()" Width="70px">
                        <Template>
                            @{
                                <input type="checkbox" Editable="true"/>
                            }
                        </Template>
                    </GridColumn>
                    break;
                default:
                    <GridColumn Field="@col.ToString()" FieldType="@typeof(string)" Width="200px" />
                    break;
            }
        }

In this case I didn't find any way to set the DateTime Format.

Thank you for your time!

0
Marin Bratanov
Telerik team
answered on 15 Sep 2020, 02:01 PM

Hi Zoya,

You can use templates in columns generated in a loop too. The key thing is that you must obtain the necessary value from the row model (in this case it would be a dictionary with the row data extracted from the DataTable as per this demo). Then you might need to cast it to the appropriate data type before you can use .ToString(format).

 

Regards,
Marin Bratanov
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).

0
Zoya
Top achievements
Rank 1
answered on 16 Sep 2020, 01:21 PM

Hi Marin,

thank you very much for your quick response. I just changed it and it works this way:

                case "System.DateTime":
                    <Telerik.Blazor.Components.GridColumn Field="@col.ToString()" FieldType="@typeof(DateTime)" Width="100px">
                        <Template>
                            @Convert.ToDateTime((context as Dictionary<string, object>)[col.ColumnName]).ToString("dd.MM.yyyy")
                        </Template>
                    </Telerik.Blazor.Components.GridColumn>
                     break;
                case "System.Boolean" :
                    <Telerik.Blazor.Components.GridColumn Field="@col.ToString()" FieldType="@typeof(bool)" Width="70px">
                        <Template>
                            @{
                                var r = Convert.ToBoolean((context as Dictionary<string, object>)[col.ColumnName]);
                                <input type="checkbox" disabled @bind=r />
                            }
                        </Template>
                    </Telerik.Blazor.Components.GridColumn>
                    break;

Have a nice evening there!

Tags
Grid
Asked by
Michael
Top achievements
Rank 2
Answers by
Marin Bratanov
Telerik team
Michael
Top achievements
Rank 2
Zoya
Top achievements
Rank 1
Share this question
or