[PR #1179] Fixed exception when set/load a filter with Nullable enum type in DataGrid #2571

Open
opened 2026-01-29 18:19:27 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/radzenhq/radzen-blazor/pull/1179

State: closed
Merged: Yes


Add a check to the Nullable enum, otherwise uses a .GetDouble(), which next calls the exception.

Error:

Error: System.AggregateException: One or more errors occurred. (Unable to cast object of type 'System.Double' to type 'System.Int32'.)
---> System.InvalidCastException: Unable to cast object of type 'System.Double' to type 'System.Int32'.
at Radzen.QueryableExtension.ToFilterString[T](IEnumerable1 columns) in \Radzen.Blazor\QueryableExtension.cs:line 188 at Radzen.Blazor.RadzenDataGrid1.InvokeLoadData(Int32 start, Int32 top) in \Radzen.Blazor\RadzenDataGrid.razor.cs:line 1827

Demo source:

@using System.Linq.Dynamic.Core
@using System.ComponentModel.DataAnnotations
@using System.Text.Json

@inject IJSRuntime JSRuntime
@inject NavigationManager NavigationManager

<RadzenDataGrid LoadData="LoadData" IsLoading=@isLoading Count="count" Data=@employees
				@bind-Settings="@Settings" LoadSettings="@LoadSettings" @ref=grid
				FilterMode="FilterMode.Simple" AllowFiltering="true" AllowPaging="true" AllowSorting="true" TItem="Employee" ColumnWidth="200px">
	<Columns>
		<RadzenDataGridColumn TItem="Employee" Property="ID" Title="ID" />
		<RadzenDataGridColumn TItem="Employee" Property="Gender" Title="Gender" />
		<RadzenDataGridColumn TItem="Employee" Property="Status" Title="Nullable Status" />
		<RadzenDataGridColumn TItem="Employee" Property="Color" Title="Favorite Color (Display Attribute in Filter)" />
	</Columns>
</RadzenDataGrid>

@code {
	int count;
	IEnumerable<Employee> initialEmployees;
	IEnumerable<Employee> employees;
	RadzenDataGrid<Employee> grid;

	bool isLoading = false;

	public class Employee
	{
		public int ID { get; set; }
		public GenderType Gender { get; set; }
		public StatusType? Status { get; set; }
		public ColorType Color { get; set; }
	}

	public enum GenderType
	{
		Ms,
		Mr,
		Unknown,
	}

	public enum ColorType
	{
		Red,
		Green,
		Blue,
		[Display(Description = "Almond Green")]
		AlmondGreen,
		[Display(Description = "Amber Gray")]
		AmberGray,
		[Display(Description = "Apple Blue... ")]
		AppleBlueSeaGreen,
		//[Display(Description = "Miss", ResourceType = typeof(ResourceFile)] localization example
		[Display(Description = "Azure")]
		AzureBlue,

	}

	public enum StatusType
	{
		Inactive,
		Active,
	}

	protected override void OnInitialized()
	{
		initialEmployees = Enumerable.Range(0, 10).Select(i =>
			new Employee
				{
					ID = i,
					Gender = i < 3 ? GenderType.Mr : i < 6 ? GenderType.Ms : GenderType.Unknown,
					Status = i < 3 ? StatusType.Active : i < 6 ? StatusType.Inactive : null,
					Color = i < 2 ? ColorType.Red : i < 4 ? ColorType.AlmondGreen : i < 6 ? ColorType.AppleBlueSeaGreen : ColorType.AzureBlue,
				});
	}

	void LoadData(LoadDataArgs args)
	{
		isLoading = true;
		var query = initialEmployees.AsQueryable();

		if (!string.IsNullOrEmpty(args.Filter))
		{
			query = query.Where(args.Filter);
		}

		if (!string.IsNullOrEmpty(args.OrderBy))
		{
			query = query.OrderBy(args.OrderBy);
		}

		count = query.Count();

		employees = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
		isLoading = false;

	}


	DataGridSettings _settings;
	public DataGridSettings Settings
	{
		get
		{
			return _settings;
		}
		set
		{
			if (_settings != value)
			{
				_settings = value;
				InvokeAsync(SaveStateAsync);
			}
		}
	}

	private async Task LoadStateAsync()
	{
		var result = await JSRuntime.InvokeAsync<string>("window.localStorage.getItem", "SettingsLoadData");
		if (!string.IsNullOrEmpty(result))
		{
			_settings = JsonSerializer.Deserialize<DataGridSettings>(result);
		}
	}

	private async Task SaveStateAsync()
	{
		await JSRuntime.InvokeVoidAsync("window.localStorage.setItem", "SettingsLoadData", JsonSerializer.Serialize<DataGridSettings>(Settings));
	}

	protected override async Task OnAfterRenderAsync(bool firstRender)
	{
		if (firstRender)
		{
			await LoadStateAsync();
		}
	}

	void LoadSettings(DataGridLoadSettingsEventArgs args)
	{
		if (Settings != null)
		{
			args.Settings = Settings;
		}
	}
}

Check column "Status"

**Original Pull Request:** https://github.com/radzenhq/radzen-blazor/pull/1179 **State:** closed **Merged:** Yes --- Add a check to the Nullable enum, otherwise uses a .GetDouble(), which next calls the exception. Error: > Error: System.AggregateException: One or more errors occurred. (Unable to cast object of type 'System.Double' to type 'System.Int32'.) > ---> System.InvalidCastException: Unable to cast object of type 'System.Double' to type 'System.Int32'. > at Radzen.QueryableExtension.ToFilterString[T](IEnumerable`1 columns) in \Radzen.Blazor\QueryableExtension.cs:line 188 > at Radzen.Blazor.RadzenDataGrid`1.InvokeLoadData(Int32 start, Int32 top) in \Radzen.Blazor\RadzenDataGrid.razor.cs:line 1827 Demo source: ``` @using System.Linq.Dynamic.Core @using System.ComponentModel.DataAnnotations @using System.Text.Json @inject IJSRuntime JSRuntime @inject NavigationManager NavigationManager <RadzenDataGrid LoadData="LoadData" IsLoading=@isLoading Count="count" Data=@employees @bind-Settings="@Settings" LoadSettings="@LoadSettings" @ref=grid FilterMode="FilterMode.Simple" AllowFiltering="true" AllowPaging="true" AllowSorting="true" TItem="Employee" ColumnWidth="200px"> <Columns> <RadzenDataGridColumn TItem="Employee" Property="ID" Title="ID" /> <RadzenDataGridColumn TItem="Employee" Property="Gender" Title="Gender" /> <RadzenDataGridColumn TItem="Employee" Property="Status" Title="Nullable Status" /> <RadzenDataGridColumn TItem="Employee" Property="Color" Title="Favorite Color (Display Attribute in Filter)" /> </Columns> </RadzenDataGrid> @code { int count; IEnumerable<Employee> initialEmployees; IEnumerable<Employee> employees; RadzenDataGrid<Employee> grid; bool isLoading = false; public class Employee { public int ID { get; set; } public GenderType Gender { get; set; } public StatusType? Status { get; set; } public ColorType Color { get; set; } } public enum GenderType { Ms, Mr, Unknown, } public enum ColorType { Red, Green, Blue, [Display(Description = "Almond Green")] AlmondGreen, [Display(Description = "Amber Gray")] AmberGray, [Display(Description = "Apple Blue... ")] AppleBlueSeaGreen, //[Display(Description = "Miss", ResourceType = typeof(ResourceFile)] localization example [Display(Description = "Azure")] AzureBlue, } public enum StatusType { Inactive, Active, } protected override void OnInitialized() { initialEmployees = Enumerable.Range(0, 10).Select(i => new Employee { ID = i, Gender = i < 3 ? GenderType.Mr : i < 6 ? GenderType.Ms : GenderType.Unknown, Status = i < 3 ? StatusType.Active : i < 6 ? StatusType.Inactive : null, Color = i < 2 ? ColorType.Red : i < 4 ? ColorType.AlmondGreen : i < 6 ? ColorType.AppleBlueSeaGreen : ColorType.AzureBlue, }); } void LoadData(LoadDataArgs args) { isLoading = true; var query = initialEmployees.AsQueryable(); if (!string.IsNullOrEmpty(args.Filter)) { query = query.Where(args.Filter); } if (!string.IsNullOrEmpty(args.OrderBy)) { query = query.OrderBy(args.OrderBy); } count = query.Count(); employees = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList(); isLoading = false; } DataGridSettings _settings; public DataGridSettings Settings { get { return _settings; } set { if (_settings != value) { _settings = value; InvokeAsync(SaveStateAsync); } } } private async Task LoadStateAsync() { var result = await JSRuntime.InvokeAsync<string>("window.localStorage.getItem", "SettingsLoadData"); if (!string.IsNullOrEmpty(result)) { _settings = JsonSerializer.Deserialize<DataGridSettings>(result); } } private async Task SaveStateAsync() { await JSRuntime.InvokeVoidAsync("window.localStorage.setItem", "SettingsLoadData", JsonSerializer.Serialize<DataGridSettings>(Settings)); } protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { await LoadStateAsync(); } } void LoadSettings(DataGridLoadSettingsEventArgs args) { if (Settings != null) { args.Settings = Settings; } } } ``` Check column "Status"
claunia added the pull-request label 2026-01-29 18:19:27 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#2571