Show value of referenced object

Hi

I have the following code:

@using System.ComponentModel.DataAnnotations

<SfGrid DataSource="@projectList">
<GridColumns>
<GridForeignColumn Field="@nameof(project.Customer)" ForeignKeyField="@nameof(customer.Id)" ForeignKeyValue="@nameof(customer.City)"
AllowSorting="true" ForeignDataSource="@customerList" HeaderText="Customer Number">
</GridForeignColumn>
<GridColumn Field=@nameof(project.Cost) HeaderText="Cost"> </GridColumn>
</GridColumns>
</SfGrid>


@code
{
List<project> projectList = new List<project>();
List<customer> customerList = new List<customer>();


protected override async Task OnInitializedAsync()
{
customerList = new List<customer>
{
new customer {Id = "1", Name = "Test1", City = "NY"},
new customer {Id = "2", Name = "Test2", City = "LA"}
};

projectList = new List<project>
{
new project
{
Customer = customerList[0],
Cost = 100
},
new project
{
Customer = customerList[1],
Cost = 150
}
};
}

public class project
{
public customer Customer { get; set; }
public int Cost { get; set; }
}

public class customer
{
[Key]
public string Id { get; set; }

public string Name { get; set; }
public string City { get; set; }
}


}
Im using EFcore 5 to generate the database schema. EF creates a column called customerId in my project table. this property is not accessible via code. only the customer object is.
Now I want to display the customers city in my grid. How do I do this?

1 Reply 1 reply marked as answer

JP Jeevakanth Palaniappan Syncfusion Team March 1, 2021 06:19 AM UTC

Hi Simon, 

Greetings from Syncfusion support. 

We have checked your query and on analyzing your code snippet, we found that you have used complex type for the customer column(public customer Customer { get; set; }). So we suggest you to refer the below documentation for binding the complex data. 


Please get back to us if you have any other queries. 

Regards, 
Jeevakanth SP. 


Marked as answer
Loader.
Up arrow icon