RadzenDataGrid & DateTime Column has filtering issues depending on the culture: System.FormatException #982

Closed
opened 2026-01-29 17:47:17 +00:00 by claunia · 8 comments
Owner

Originally created by @mikoskinen on GitHub (Sep 11, 2023).

Describe the bug

Request's culture decides if the DateTime column filtering works or crashes with System.FormatException when using RadzenDataGrid.

To Reproduce

I created a repo here, the grid is in Index.razor: https://github.com/mikoskinen/radzen-datetime-filter-formatexception

If you try to filter the first column, you should see:

Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
Unhandled exception in circuit 'DLeDn5N2V6zAy3RzR1o--9MbI23_KXbSSn7khDPTYCs'.
System.FormatException: String '2023-09-05T00.00.00.000Z' was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(ReadOnlySpan1 s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles) at Radzen.QueryableExtension.GetColumnFilter[T](RadzenDataGridColumn1 column, String value, Boolean second)
at Radzen.QueryableExtension.ToFilterString[T](IEnumerable1 columns) at Radzen.Blazor.RadzenDataGrid1.InvokeLoadData(Int32 start, Int32 top)
at Radzen.Blazor.RadzenDataGrid1.Reload() at Radzen.Blazor.RadzenDataGrid1.OnFilter(ChangeEventArgs args, RadzenDataGridColumn1 column, Boolean force, Boolean isFirst) at Radzen.Blazor.RadzenDataGrid1.ApplyFilter(RadzenDataGridColumn`1 column, Boolean closePopup)
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

Now if you go to Program.cs you can see that I've set default request localization to fi-FI:

app.UseRequestLocalization(new RequestLocalizationOptions().SetDefaultCulture("fi-FI"));

Change this to en-US and the filtering works OK.

Expected behavior

The datetime filtering should work with other culture's than en-US.

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser: Firefox
  • Version: 117

Also tested with Edge.

Additional context

I'm guess think the issue is related to this: cae80096e1/Radzen.Blazor/QueryableExtension.cs (L470C69-L470C69)

The parsing expects to get the datetime in Invariant Culture format but it doesn't. So maybe change this to parse the datetime using the request's culture or change the filter to produce the value in invariant culture.

Originally created by @mikoskinen on GitHub (Sep 11, 2023). **Describe the bug** Request's culture decides if the DateTime column filtering works or crashes with System.FormatException when using RadzenDataGrid. **To Reproduce** I created a repo here, the grid is in Index.razor: https://github.com/mikoskinen/radzen-datetime-filter-formatexception If you try to filter the first column, you should see: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111] Unhandled exception in circuit 'DLeDn5N2V6zAy3RzR1o--9MbI23_KXbSSn7khDPTYCs'. System.FormatException: String '2023-09-05T00.00.00.000Z' was not recognized as a valid DateTime. at System.DateTimeParse.Parse(ReadOnlySpan`1 s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles) at Radzen.QueryableExtension.GetColumnFilter[T](RadzenDataGridColumn`1 column, String value, Boolean second) at Radzen.QueryableExtension.ToFilterString[T](IEnumerable`1 columns) at Radzen.Blazor.RadzenDataGrid`1.InvokeLoadData(Int32 start, Int32 top) at Radzen.Blazor.RadzenDataGrid`1.Reload() at Radzen.Blazor.RadzenDataGrid`1.OnFilter(ChangeEventArgs args, RadzenDataGridColumn`1 column, Boolean force, Boolean isFirst) at Radzen.Blazor.RadzenDataGrid`1.ApplyFilter(RadzenDataGridColumn`1 column, Boolean closePopup) at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState) Now if you go to Program.cs you can see that I've set default request localization to fi-FI: app.UseRequestLocalization(new RequestLocalizationOptions().SetDefaultCulture("fi-FI")); Change this to en-US and the filtering works OK. **Expected behavior** The datetime filtering should work with other culture's than en-US. **Desktop (please complete the following information):** - OS: Windows 10 - Browser: Firefox - Version: 117 Also tested with Edge. **Additional context** I'm guess think the issue is related to this: https://github.com/radzenhq/radzen-blazor/blob/cae80096e12802de1498857272f0bce5f3b3bd77/Radzen.Blazor/QueryableExtension.cs#L470C69-L470C69 The parsing expects to get the datetime in Invariant Culture format but it doesn't. So maybe change this to parse the datetime using the request's culture or change the filter to produce the value in invariant culture.
Author
Owner

@enchev commented on GitHub (Sep 11, 2023):

Hey @mikoskinen,

Sounds like a problem with fi-FI culture only, worked normally with every other culture I've tested.

@enchev commented on GitHub (Sep 11, 2023): Hey @mikoskinen, Sounds like a problem with `fi-FI` culture only, worked normally with every other culture I've tested.
Author
Owner

@mikoskinen commented on GitHub (Sep 11, 2023):

Hi @enchev,

Thank you for the quick reply! There's at least one other culture which has the same issue. I'm not sure why some are working and some aren't...

da-DK:

image

There's some other reports which seem to be related to this. I picked the da-DK from one of these:

https://forum.radzen.com/t/culture-on-datepicker-in-filter/14644/4
https://forum.radzen.com/t/datagrid-date-column-filter-exception/14401
https://forum.radzen.com/t/problem-with-culture-in-date-filter/14640

Best regards,
Mikael

@mikoskinen commented on GitHub (Sep 11, 2023): Hi @enchev, Thank you for the quick reply! There's at least one other culture which has the same issue. I'm not sure why some are working and some aren't... da-DK: ![image](https://github.com/radzenhq/radzen-blazor/assets/143570/74beefcd-1d93-4fbf-858f-8e29d05f73e4) There's some other reports which seem to be related to this. I picked the da-DK from one of these: https://forum.radzen.com/t/culture-on-datepicker-in-filter/14644/4 https://forum.radzen.com/t/datagrid-date-column-filter-exception/14401 https://forum.radzen.com/t/problem-with-culture-in-date-filter/14640 Best regards, Mikael
Author
Owner

@akorchev commented on GitHub (Sep 11, 2023):

@mikoskinen does it work if you specify CultureInfo.InvariantCulture as a DateTime.Parse argument?

@akorchev commented on GitHub (Sep 11, 2023): @mikoskinen does it work if you specify `CultureInfo.InvariantCulture` as a DateTime.Parse argument?
Author
Owner

@mikoskinen commented on GitHub (Sep 12, 2023):

Hi @akorchev,

The particular code has CultureInfo.InvariantCulture as the argument, which makes it more strange. I'm not sure if you meant that, sorry if I misunderstood. This is the line in question:

var dateTimeValue = DateTime.Parse(value, CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind);

Here's couple screenshots where I debug the external code:

First with SetDefaultCulture("fi-FI"):

image

This crashes with: System.FormatException: String '2023-09-04T00.00.00.000Z' was not recognized as a valid DateTime.

Now with SetDefaultCulture("en-US"):

image

This works, doesn't throw an exception.

You can see that the "value"'s format changes in these cases. The time part uses the separator . in fi-FI and separator : in en-US. In valid ISO-8601 the separator should be :

It seems that the code which is producing the string '2023-09-04T00.00.00.000Z' isn't actually creating correctly formatted ISO-8601 string.

@mikoskinen commented on GitHub (Sep 12, 2023): Hi @akorchev, The particular code has CultureInfo.InvariantCulture as the argument, which makes it more strange. I'm not sure if you meant that, sorry if I misunderstood. This is the line in question: `var dateTimeValue = DateTime.Parse(value, CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind);` Here's couple screenshots where I debug the external code: First with SetDefaultCulture("fi-FI"): ![image](https://github.com/radzenhq/radzen-blazor/assets/143570/fd385f72-7c5e-4c7e-949d-77160020e787) This crashes with: System.FormatException: String '2023-09-04T00.00.00.000Z' was not recognized as a valid DateTime. Now with SetDefaultCulture("en-US"): ![image](https://github.com/radzenhq/radzen-blazor/assets/143570/1e03c350-f2aa-41dd-bf74-606c357ec08d) This works, doesn't throw an exception. You can see that the "value"'s format changes in these cases. The time part uses the separator . in fi-FI and separator : in en-US. In valid ISO-8601 the separator should be : It seems that the code which is producing the string '2023-09-04T00.00.00.000Z' isn't actually creating correctly formatted ISO-8601 string.
Author
Owner

@mikoskinen commented on GitHub (Sep 12, 2023):

Ah, I think I found it. It is this code block: cae80096e1/Radzen.Blazor/QueryableExtension.cs (L171)

The problem is that ToString should use either InvariantCulture (even if it seems to be specifying the exact format) or format "o".

I created a simple C# console app to test:

var d = new DateTime(2023, 05, 03).ToUniversalTime();
var val = d.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");

And:

image

So even if you seem to be defining the exact format, the output still contains . instead of :

There's couple solutions:

  1. Use CultureInfo.InvariantCulture when outputting the string, even though you are using the exactly specified format:

image

  1. Our output using "o" format:

image

What do you think?

edit: I was able to find the related documentation from MS: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#date-and-time-separator-specifiers

@mikoskinen commented on GitHub (Sep 12, 2023): Ah, I think I found it. It is this code block: https://github.com/radzenhq/radzen-blazor/blob/cae80096e12802de1498857272f0bce5f3b3bd77/Radzen.Blazor/QueryableExtension.cs#L171 The problem is that ToString should use either InvariantCulture (even if it seems to be specifying the exact format) or format "o". I created a simple C# console app to test: var d = new DateTime(2023, 05, 03).ToUniversalTime(); var val = d.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); And: ![image](https://github.com/radzenhq/radzen-blazor/assets/143570/8a7101aa-9615-43ff-8869-cd74bbf0458a) So even if you seem to be defining the exact format, the output still contains . instead of : There's couple solutions: 1. Use CultureInfo.InvariantCulture when outputting the string, even though you are using the exactly specified format: ![image](https://github.com/radzenhq/radzen-blazor/assets/143570/aeeff069-1e77-4896-8a36-c8ea764963b7) 2. Our output using "o" format: ![image](https://github.com/radzenhq/radzen-blazor/assets/143570/88af77b0-79eb-4e72-ab3c-98bf5a01014e) What do you think? edit: I was able to find the related documentation from MS: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#date-and-time-separator-specifiers
Author
Owner

@enchev commented on GitHub (Sep 12, 2023):

Thanks @mikoskinen! Fix will be published later today!

@enchev commented on GitHub (Sep 12, 2023): Thanks @mikoskinen! Fix will be published later today!
Author
Owner

@enchev commented on GitHub (Sep 12, 2023):

f0a987e7a2

@enchev commented on GitHub (Sep 12, 2023): https://github.com/radzenhq/radzen-blazor/commit/f0a987e7a25f08bb035342d89d6e53b2097c7803
Author
Owner

@mikoskinen commented on GitHub (Sep 12, 2023):

Thank you for the support and for the excellent project!

@mikoskinen commented on GitHub (Sep 12, 2023): Thank you for the support and for the excellent project!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#982