So it's not possible to have an int in a database and map this to an enum to show user friendly text and be filterable with the default datagrid?
https://blazor.radzen.com/datagrid-enum-filter
Here it seems to be working fine.
All of this code is in one page though.
I have a Blazor 8 with Auto Interactive and PWA.
There is a Server project and Client project.
I'm so confused.
If I change my field type back to int in the model, it works as expected.
[Table("Accounts", Schema = "dbo")]
public partial class Account
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string AccountName { get; set; }
[Required]
[EnumDataType(typeof(AccountType))]
public AccountType AccountType { get; set; }
// I changed this from int to AccountType
[Required]
public decimal AccountBalance { get; set; }
[Required]
public int AccountNumber { get; set; }
public ICollection<TransactionEntry> TransactionEntries { get; set; }
}
public enum AccountType
{
Asset = 1000,
Liability = 2000,
Equity = 3000,
Revenue = 4000,
Expense = 6000,
COGS = 5000
}
With the property type set to AccountType, the items won't load in the grid.
Accounts.razor
@page "/accounts"
@inject IJSRuntime JSRuntime
@inject NavigationManager NavigationManager
@inject DialogService DialogService
@inject ContextMenuService ContextMenuService
@inject TooltipService TooltipService
@inject NotificationService NotificationService
@inject BlazorPortalDbService BlazorPortalDbService
Accounts
<RadzenTextBox Placeholder="Search ..." style="display: block; width: 100%" @oninput="@Search" aria-label="Search by all string columns" />
<RadzenDataGrid @ref="grid0" ColumnWidth="200px" AllowFiltering="true" FilterMode="FilterMode.Advanced" AllowPaging="true" AllowSorting="true" ShowPagingSummary="true" PageSizeOptions=@(new int{5, 10, 20, 30})
Data="@accounts" LoadData="@Grid0LoadData" Count="@count" TItem="BlazorPortal.Server.Models.BlazorPortalDb.Account" RowDoubleClick="@EditRow">
<RadzenButton ButtonStyle="ButtonStyle.Danger" Icon="delete" Size="ButtonSize.Medium"
Shade="Shade.Lighter" Variant="Variant.Flat"
Click=@(args => GridDeleteButtonClick(args, account)) @onclick:stopPropagation="true" />
</Template>
</RadzenDataGridColumn>
</Columns>
</RadzenDataGrid>
</RadzenColumn>
@code {
protected IEnumerable<BlazorPortal.Server.Models.BlazorPortalDb.Account> accounts;
protected RadzenDataGrid<BlazorPortal.Server.Models.BlazorPortalDb.Account> grid0;
protected int count;
protected string search = "";
protected async Task Search(ChangeEventArgs args)
{
search = $"{args.Value}";
await grid0.GoToPage(0);
await grid0.Reload();
}
protected async Task Grid0LoadData(LoadDataArgs args)
{
try
{
var result = await BlazorPortalDbService.GetAccounts(filter: $@"(contains(AccountName,""{search}"")) and {(string.IsNullOrEmpty(args.Filter)? "true" : args.Filter)}", orderby: $"{args.OrderBy}", top: args.Top, skip: args.Skip, count:args.Top != null && args.Skip != null);
accounts = result.Value.AsODataEnumerable();
count = result.Count;
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
NotificationService.Notify(new NotificationMessage(){ Severity = NotificationSeverity.Error, Summary = $"Error", Detail = $"Unable to load Accounts" });
}
}
protected async Task AddButtonClick(MouseEventArgs args)
{
await DialogService.OpenAsync<AddAccount>("Add Account", null);
await grid0.Reload();
}
protected async Task EditRow(DataGridRowMouseEventArgs<BlazorPortal.Server.Models.BlazorPortalDb.Account> args)
{
await DialogService.OpenAsync<EditAccount>("Edit Account", new Dictionary<string, object> { {"Id", args.Data.Id} });
await grid0.Reload();
}
protected async Task GridDeleteButtonClick(MouseEventArgs args, BlazorPortal.Server.Models.BlazorPortalDb.Account account)
{
try
{
if (await DialogService.Confirm("Are you sure you want to delete this record?") == true)
{
var deleteResult = await BlazorPortalDbService.DeleteAccount(id:account.Id);
if (deleteResult != null)
{
await grid0.Reload();
}
}
}
catch (Exception ex)
{
NotificationService.Notify(new NotificationMessage
{
Severity = NotificationSeverity.Error,
Summary = $"Error",
Detail = $"Unable to delete Account"
});
}
}
protected async Task ExportClick(RadzenSplitButtonItem args)
{
if (args?.Value == "csv")
{
await BlazorPortalDbService.ExportAccountsToCSV(new Query
{
Filter = $@"{(string.IsNullOrEmpty(grid0.Query.Filter)? "true" : grid0.Query.Filter)}",
OrderBy = $"{grid0.Query.OrderBy}",
Expand = "",
Select = string.Join(",", grid0.ColumnsCollection.Where(c => c.GetVisible() && !string.IsNullOrEmpty(c.Property)).Select(c => c.Property.Contains(".") ? c.Property + " as " + c.Property.Replace(".", "") : c.Property))
}, "Accounts");
}
if (args == null || args.Value == "xlsx")
{
await BlazorPortalDbService.ExportAccountsToExcel(new Query
{
Filter = $@"{(string.IsNullOrEmpty(grid0.Query.Filter)? "true" : grid0.Query.Filter)}",
OrderBy = $"{grid0.Query.OrderBy}",
Expand = "",
Select = string.Join(",", grid0.ColumnsCollection.Where(c => c.GetVisible() && !string.IsNullOrEmpty(c.Property)).Select(c => c.Property.Contains(".") ? c.Property + " as " + c.Property.Replace(".", "") : c.Property))
}, "Accounts");
}
}
}