Trouble displaying datetime in GridColumn

1 Answer 223 Views
Grid
Henrik
Top achievements
Rank 1
Henrik asked on 25 Apr 2023, 02:57 PM

Hi, I'm trying to display the local datetime using this code but it just shows up empty.

 <GridColumn  Field="@(nameof(Dto.DateTimeOffset.LocalDateTime))" Title="Date" />
If I remove .LocalDateTime it works but then it shows the incorrect local time.

1 Answer, 1 is accepted

Sort by
1
Accepted
Justin
Telerik team
answered on 25 Apr 2023, 06:33 PM

Hi Henrik,

Try calling the LocalDateTime method on the Dto.DateTimeOffset field in a Column Template like this: 

<GridColumn Field="@nameof(Product.Released)" Title="Released" >
            <Template>
                @{
                    var product = context as Product;
                    var time = product.Released;
                    <span>@time.LocalDateTime.ToString("F")</span>
                }
            </Template>
        </GridColumn>

Here is a running example in our REPL.

I hope this helps.

Regards,
Justin
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Henrik
Top achievements
Rank 1
commented on 25 Apr 2023, 07:44 PM

Thanks for the quick reply.

Is there anyway to simplify/reuse it because I need todo it in a lot of places with multiple dates per grid?

 

Regards,

Henrik

 
Dimo
Telerik team
commented on 28 Apr 2023, 08:35 AM

@Henrik - there are a couple of options -

  • Add one more property in the Grid model class, which has a custom getter and returns the above expression. Bind the column to that property. In this case you won't need a column template, but all data operations for this column will start working on a string property.
  • Implement reusable Grid column template.
Tags
Grid
Asked by
Henrik
Top achievements
Rank 1
Answers by
Justin
Telerik team
Share this question
or