I'm trying to implement <GridForeignColumn> in a Datagrid with Guids as foreign keys and it throws an
Object reference not set to an instance of an object error. What am I doing wrong?
Here's my code:
@page "/tarifflist"
@using GridForeignColumnError.Data
@using Microsoft.EntityFrameworkCore
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.DropDowns
@using Syncfusion.Blazor
@inject MyDbContext dbContext
<h3>Tariff List</h3>
<SfGrid DataSource="@tariffList" >
<GridColumns>
<GridColumn Field="@nameof(Tariff.Name)" HeaderText="Name" ValidationRules="@(new ValidationRules{ Required=true })"></GridColumn>
<!-- GridForeignColumn Object reference not set to an instance of an object -->
<GridForeignColumn Field="@nameof(Tariff.UomId)" HeaderText="UOM" ForeignKeyValue="Code" ForeignDataSource="@uomList"></GridForeignColumn>
<GridColumn Field="@nameof(Tariff.Number)" HeaderText="Tariff Number" ValidationRules="@(new ValidationRules{ Required=true })"></GridColumn>
<GridColumn Field="@nameof(Tariff.GeneralRate)" HeaderText="General Rate" ValidationRules="@(new ValidationRules{ Required=true })"></GridColumn>
<GridColumn Field="@nameof(Tariff.ExciseRate)" HeaderText="Excise Rate" ValidationRules="@(new ValidationRules{ Required=true })"></GridColumn>
</GridColumns>
</SfGrid>
@code {
private List<Uom> uomList { get; set; }
public List<Tariff> tariffList { get; set; }
protected override void OnInitialized()
{
tariffList = dbContext.Tariff
.Include(t => t.Uom)
.ToList();
uomList = dbContext.Uom.ToList();
}
}
Hi Ryan,
Greetings from Syncfusion support.
Query: “I'm trying to implement <GridForeignColumn> in a Datagrid with Guids as foreign keys and it throws an Object reference not set to an instance of an object error. What am I doing wrong?”
We have checked your query and we would like to inform that by using Grid foreign key column we can perform data-mapping between two tables (DataSource) by using column Field and ForeignKeyField. Based on the corresponding ForeignKeyField value it maps the value in Grid DataSource. If both ForeignKey & Grid data have same mapping value, then we can display the corresponding value in Grid. Kindly refer the below code snippet for your reference.
From your shared sample we could see that there is no common fields between GridData source and ForeignKeyData source so the reported issue occurs. Also we would like to inform that both the grid and Foreignkey column data should have same value. So that it maps the corresponding data value in foreign key table.
|
<SfGrid DataSource="@Orders" Height="315"> <GridColumns> <GridForeignColumn Field=@nameof(Order.EmployeeID) HeaderText="Employee Name" ForeignKeyValue="FirstName" ForeignDataSource="@Employees" Width="150"></GridForeignColumn>
</GridColumns> </SfGrid>
@code{ public List<Order> Orders { get; set; } public List<EmployeeData> Employees { get; set; }
protected override void OnInitialized() { Orders = Enumerable.Range(1, 75).Select(x => new Order() { OrderID = 1000 + x, EmployeeID = x, }).ToList();
Employees = Enumerable.Range(1, 75).Select(x => new EmployeeData() { EmployeeID = x, FirstName = (new string[] { "Nancy", "Andrew", "Janet", "Margaret", "Steven" })[new Random().Next(5)], }).ToList(); }
public class Order { public int? OrderID { get; set; } public int? EmployeeID { get; set; } public DateTime? OrderDate { get; set; } public double? Freight { get; set; } }
public class EmployeeData { public int? EmployeeID { get; set; } public string FirstName { get; set; } }
|
|
<SfGrid DataSource="@tariffList" > <GridColumns> <GridColumn Field="@nameof(Tariff.Name)" HeaderText="Name" ValidationRules="@(new ValidationRules{ Required=true })"></GridColumn> <!-- GridForeignColumn Object reference not set to an instance of an object --> <GridForeignColumn Field="@nameof(Tariff.UomId)" HeaderText="UOM" ForeignKeyValue="Code" ForeignDataSource="@uomList"></GridForeignColumn>
</GridColumns> </SfGrid>
public class Tariff { public Guid Id { get; set; } public string Name { get; set; } public double Number { get; set; } public double GeneralRate { get; set; } public double ExciseRate { get; set; } public Guid? UomId { get; set; } public Uom? Uom { get; set; } } #endregion
#region UOM public class Uom { public Guid Id { get; set; } public string Code { get; set; } public string? Description { get; set; } } |
Reference: https://blazor.syncfusion.com/documentation/datagrid/columns#foreign-key-column
Kindly get back to us if you have further queries.
Regards,
Monisha
Hi Monisha,
Thanks for your quick reply! And thanks for pointing out my error. I didn't understand that the foreign key Id field name had to match. Thanks for pointing that out and it works for me now.
Regards,
Ryan
Hi Ryan,
Welcome.
We are glad to hear that the reported query has been resolved. Kindly get back to us if you have further queries. As always we will be happy to help you.
Regards,
Monisha