Error when loading/saving RadzenDataGrid Settings, where there are filterable columns without Property #921

Closed
opened 2026-01-29 17:46:22 +00:00 by claunia · 3 comments
Owner

Originally created by @lulikin on GitHub (Jul 13, 2023).

I have a class which inherit from RadzenDataGridColumn and override GetColumnODataFilter.

        protected override string GetColumnODataFilter(object filterValue, FilterOperator filterOperator)
        {
            var property = GetFilterProperty().Replace('.', '/');

            return $"{property}/any(x: x/Value eq '{filterValue}' and x/CustomPropertyId eq {CustomPropertyId})";
        }

Then I use this in my Grid like

                   <ContactCustomPropertyGridColumn FilterProperty="@nameof(Flegis.Model.Contact.ContactCustomProperties)" TItem="Flegis.Model.Contact" CustomPropertyId="@(customProperty.Id)" Title="@customProperty.Name" Frozen="false" Sortable="false" Filterable="true" Width="100px" TextAlign="TextAlign.Left">
                       <Template Context="data">
                           @switch (customProperty.AnswerType)
                           {
                               case Model.AnswerType.Note:
                               case Model.AnswerType.Text:
                                   @(data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id) ? data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id).Value : "")
                                   break;
                               case Model.AnswerType.Bool:
                                   <RadzenCheckBox TriState="true" TValue="bool?" Value="@(data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id) ? data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id).IntValue==1 : null)" />
                                   break;
                               case Model.AnswerType.ChooseMany:
                                   break;
                               case Model.AnswerType.ChooseOne:
                                   if (data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id))
                                   {
                                       var value = data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id);
                                       if (value.IntValue.HasValue)
                                       {
                                           <p>@customProperty.CustomPropertyAnswers.First(f => f.Value  == value.IntValue.Value).Title</p>
                                       }
                                   }
                                   else
                                   {
                                   }
                                   break;
                           }
                       </Template>
                   </ContactCustomPropertyGridColumn>

When page load I get this error

blazor.webassembly.js:1  crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Radzen.PropertyAccess.Getter[Contact,Object](String propertyName, Type type)
   at Radzen.Blazor.RadzenDataGridColumn`1[[Flegis.Model.Contact, Flegis.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnInitialized()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Radzen.Blazor.RadzenDataGridColumn`1.<SetParametersAsync>d__192[[Flegis.Model.Contact, Flegis.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()

Filtering works like it should. If I remove that column I don't get any error. If I remove binding for settings from grid error also disappear.

My line for Grid:
<RadzenDataGrid @ref="_CustomersGrid" @bind-Settings="GridSettings" IsLoading="@isLoading" Count="@_TotalResults" Data="@_Customers" LoadData="@LoadData" FilterPopupRenderMode="PopupRenderMode.OnDemand" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" FilterMode="FilterMode.SimpleWithMenu" AllowSorting="true" AllowFiltering="!CustomFilter" AllowPaging="true" PageSize="_PageSize" PagerHorizontalAlign="HorizontalAlign.Center" TItem="Customer" ColumnWidth="200px" AllowColumnPicking="true" AllowColumnResize="true" AllowColumnReorder="true">

Like you see, I get values for the column from related List. If I add Property="..." to the column error tells me, that Property does not exists for my class. Which is correct.

I suspect, that error saving settings use Property for key, but I cannot set it, because Property does not exists for my class. Solution could be adding new Parameter to RadzenDataGridColumn, which will be used for saving settings.

Originally created by @lulikin on GitHub (Jul 13, 2023). I have a class which inherit from RadzenDataGridColumn<TItem> and override GetColumnODataFilter. ``` protected override string GetColumnODataFilter(object filterValue, FilterOperator filterOperator) { var property = GetFilterProperty().Replace('.', '/'); return $"{property}/any(x: x/Value eq '{filterValue}' and x/CustomPropertyId eq {CustomPropertyId})"; } ``` Then I use this in my Grid like ``` <ContactCustomPropertyGridColumn FilterProperty="@nameof(Flegis.Model.Contact.ContactCustomProperties)" TItem="Flegis.Model.Contact" CustomPropertyId="@(customProperty.Id)" Title="@customProperty.Name" Frozen="false" Sortable="false" Filterable="true" Width="100px" TextAlign="TextAlign.Left"> <Template Context="data"> @switch (customProperty.AnswerType) { case Model.AnswerType.Note: case Model.AnswerType.Text: @(data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id) ? data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id).Value : "") break; case Model.AnswerType.Bool: <RadzenCheckBox TriState="true" TValue="bool?" Value="@(data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id) ? data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id).IntValue==1 : null)" /> break; case Model.AnswerType.ChooseMany: break; case Model.AnswerType.ChooseOne: if (data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id)) { var value = data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id); if (value.IntValue.HasValue) { <p>@customProperty.CustomPropertyAnswers.First(f => f.Value == value.IntValue.Value).Title</p> } } else { } break; } </Template> </ContactCustomPropertyGridColumn> ``` When page load I get this error ``` blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Object reference not set to an instance of an object. System.NullReferenceException: Object reference not set to an instance of an object. at Radzen.PropertyAccess.Getter[Contact,Object](String propertyName, Type type) at Radzen.Blazor.RadzenDataGridColumn`1[[Flegis.Model.Contact, Flegis.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnInitialized() at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync() at Radzen.Blazor.RadzenDataGridColumn`1.<SetParametersAsync>d__192[[Flegis.Model.Contact, Flegis.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext() ``` Filtering works like it should. If I remove that column I don't get any error. If I remove binding for settings from grid error also disappear. My line for Grid: ` <RadzenDataGrid @ref="_CustomersGrid" @bind-Settings="GridSettings" IsLoading="@isLoading" Count="@_TotalResults" Data="@_Customers" LoadData="@LoadData" FilterPopupRenderMode="PopupRenderMode.OnDemand" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" FilterMode="FilterMode.SimpleWithMenu" AllowSorting="true" AllowFiltering="!CustomFilter" AllowPaging="true" PageSize="_PageSize" PagerHorizontalAlign="HorizontalAlign.Center" TItem="Customer" ColumnWidth="200px" AllowColumnPicking="true" AllowColumnResize="true" AllowColumnReorder="true">` Like you see, I get values for the column from related List. If I add Property="..." to the column error tells me, that Property does not exists for my class. Which is correct. I suspect, that error saving settings use Property for key, but I cannot set it, because Property does not exists for my class. Solution could be adding new Parameter to RadzenDataGridColumn, which will be used for saving settings.
Author
Owner

@enchev commented on GitHub (Jul 13, 2023):

Indeed Property is used to identify the column. I see that you've set FilterProperty="@nameof(Flegis.Model.Contact.ContactCustomProperties)" maybe you can do that for Property as well?

@enchev commented on GitHub (Jul 13, 2023): Indeed `Property` is used to identify the column. I see that you've set `FilterProperty="@nameof(Flegis.Model.Contact.ContactCustomProperties)"` maybe you can do that for `Property` as well?
Author
Owner

@lulikin commented on GitHub (Jul 13, 2023):

No, I can't. In reality I have many columns from another table. I have Contact with properties. Here is code.

@if (_ContactCustomProperties != null)
{
    foreach (var customProperty in _ContactCustomProperties.OrderBy(o => o.Order))
    {
        <ContactCustomPropertyGridColumn FilterProperty="@nameof(Flegis.Model.Contact.ContactCustomProperties)" TItem="Flegis.Model.Contact" CustomPropertyId="@(customProperty.Id)" Title="@customProperty.Name" Frozen="false" Sortable="false" Filterable="true" Width="100px" TextAlign="TextAlign.Left">
            <Template Context="data">
                @switch (customProperty.AnswerType)
                {
                    case Model.AnswerType.Note:
                    case Model.AnswerType.Text:
                        @(data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id) ? data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id).Value : "")
                        break;
                    case Model.AnswerType.Bool:
                        <RadzenCheckBox TriState="true" TValue="bool?" Value="@(data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id) ? data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id).IntValue==1 : null)" />
                        break;
                    case Model.AnswerType.ChooseMany:
                        break;
                    case Model.AnswerType.ChooseOne:
                        if (data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id))
                        {
                            var value = data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id);
                            if (value.IntValue.HasValue)
                            {
                                <p>@customProperty.CustomPropertyAnswers.First(f => f.Value  == value.IntValue.Value).Title</p>
                            }
                        }
                        else
                        {
                        }
                        break;
                }
            </Template>
        </ContactCustomPropertyGridColumn>
    }
}

In that case I would have many columns, with same Property.

@lulikin commented on GitHub (Jul 13, 2023): No, I can't. In reality I have many columns from another table. I have Contact with properties. Here is code. ``` @if (_ContactCustomProperties != null) { foreach (var customProperty in _ContactCustomProperties.OrderBy(o => o.Order)) { <ContactCustomPropertyGridColumn FilterProperty="@nameof(Flegis.Model.Contact.ContactCustomProperties)" TItem="Flegis.Model.Contact" CustomPropertyId="@(customProperty.Id)" Title="@customProperty.Name" Frozen="false" Sortable="false" Filterable="true" Width="100px" TextAlign="TextAlign.Left"> <Template Context="data"> @switch (customProperty.AnswerType) { case Model.AnswerType.Note: case Model.AnswerType.Text: @(data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id) ? data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id).Value : "") break; case Model.AnswerType.Bool: <RadzenCheckBox TriState="true" TValue="bool?" Value="@(data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id) ? data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id).IntValue==1 : null)" /> break; case Model.AnswerType.ChooseMany: break; case Model.AnswerType.ChooseOne: if (data.ContactCustomProperties.Any(a => a.CustomPropertyId == customProperty.Id)) { var value = data.ContactCustomProperties.First(a => a.CustomPropertyId == customProperty.Id); if (value.IntValue.HasValue) { <p>@customProperty.CustomPropertyAnswers.First(f => f.Value == value.IntValue.Value).Title</p> } } else { } break; } </Template> </ContactCustomPropertyGridColumn> } } ``` In that case I would have many columns, with same Property.
Author
Owner

@lulikin commented on GitHub (Jul 14, 2023):

Not elegant solution, but I solve that.
In class I add

[JsonIgnore]
[NotMapped]
public bool[] Dummy {get;set;}

In Blazor i change the line to
<ContactCustomPropertyGridColumn Property="@($"{nameof(Flegis.Model.Contact.Dummy)}[{customProperty.Name.GetHashCode()}]")" FilterProperty="@nameof(Flegis.Model.Contact.ContactCustomProperties)" TItem="Flegis.Model.Contact" CustomProperty="@(customProperty)" Title="@customProperty.Name" Frozen="false" Sortable="false" Filterable="true" Width="100px" TextAlign="TextAlign.Left">

Not elegant, but it works.

@lulikin commented on GitHub (Jul 14, 2023): Not elegant solution, but I solve that. In class I add ``` [JsonIgnore] [NotMapped] public bool[] Dummy {get;set;} ``` In Blazor i change the line to ` <ContactCustomPropertyGridColumn Property="@($"{nameof(Flegis.Model.Contact.Dummy)}[{customProperty.Name.GetHashCode()}]")" FilterProperty="@nameof(Flegis.Model.Contact.ContactCustomProperties)" TItem="Flegis.Model.Contact" CustomProperty="@(customProperty)" Title="@customProperty.Name" Frozen="false" Sortable="false" Filterable="true" Width="100px" TextAlign="TextAlign.Left">` Not elegant, but it works.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#921