Hi, I have an issue with the SfDatagrid when setting the value of the property 'Field', in the grid, to a property of a child model.
This is the error I'm getting:
Error: System.NullReferenceException: Object reference not set to an instance of an object.
at Syncfusion.Blazor.Grids.Internal.PropertyInfoHelper.CreateAccessor(PropertyInfo propertyInfo)
at Syncfusion.Blazor.Grids.Internal.PropertyInfoHelper.GetAccessor(String name, IDictionary`2 cache, Object from)
at Syncfusion.Blazor.Grids.Internal.PropertyInfoHelper.GetFieldValue(String nameSpace, Object from, IDictionary`2 cache)
at Syncfusion.Blazor.Grids.Internal.PropertyInfoHelper.GetObject(String nameSpace, Object from)
at Syncfusion.Blazor.Grids.Internal.GridCellBase`1.GetValue()
at Syncfusion.Blazor.Grids.Internal.GridCellBase`1.OnParametersSetAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
These are my models:
public class MenuProduct
{
public Guid Id { get; set; }
public decimal Price { get; set; }
public Product Product { get; set; }
}
public class Product
{
public Guid Id { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
public decimal DefaultPrice { get; set; }
public bool IsInactive { get; set; } = false;
}
Grid:
<SfGrid DataSource="menuProducts.ToList()"
AllowPaging="false" Toolbar="@ToolBarItems" @ref="Grid"
EnableHover="false"
AllowSorting="true">
<GridEditSettings AllowAdding="false"
AllowEditing="true"
AllowDeleting="false"
Mode="EditMode.Normal" />
<GridPageSettings PageSize="15" PageSizes="true" />
<GridTemplates>
<EmptyRecordTemplate>
No products have been entered for this category yet
</EmptyRecordTemplate>
</GridTemplates>
<GridColumns>
<GridColumn Visible="false"
IsPrimaryKey="true"
Field="@nameof(MenuProduct.Id)" />
<GridColumn HeaderText="Product Name" Width="70"
Field="@nameof(MenuProduct.Product.ProductName)"> <-- ERROR OCCURS HERE
<Template>
@{
var val = (context as MenuProduct).Product.ProductName;
<span>@val</span>
}
</Template>
</GridColumn>
</SfGrid>
I can get the value to be shown, of the 'MenuProduct.Product.ProductName', when it is within a template but if I set it using the 'Field' property like this:
Field="@nameof(MenuProduct.Product.ProductName)"> I get the mentioned error.
This seems to happen in properties of string type. If I set the field to a bool or datetime property instead, it works fine.
Field="@nameof(MenuProduct.Product.IsInactive)"> <--it works fine.
I could use the template to display the value, however, the sorting in the column wont work, so I need to set the Field.
@code {
private List<MenuProduct> menuProducts { get; set; }
private SfGrid<MenuProduct> Grid { get; set; }
private string[] ToolBarItems = new string[] { "Edit", "Update", "Cancel" };
protected override async Task OnInitializedAsync()
{
// await base.OnInitializedAsync();
await Task.CompletedTask;
menuProducts = await _menuproductRepo.GetMenuProductsFilter(p => p.Category.Id == Category.Id);
// Ive checke the data received in menuProducts and the data is all there.
}
Any ideas??
Thanks!