How to get model data of sfDataGrid

Hello everyone.
I am sfDataGrid in Blazor Server App to display data. My App has two related models Category and Subcategory.
I want to display rows of Subcategories in the sfDataGrid. In stead of displaying the CategoryId(which is the foreign key) in Subcategory, I want to display the category name. I try to use CategoryId property of subcategory model as show in code block shown below but it throws null reference error. Apparently because it does get set.
I should be very grateful I anyone is able to help me out. Relevant section of my code is shown below
[Inject]
public ISubCategoryService subcategoryService { get; set; }
[Inject]
public ICategoryService categoryService { get; set; }
public List GetSubCategories { get; set; }
public SubCategoryForDetails GetCategory { get; set; }
public SubCategoryForDetails subcategory { get; set; } = new SubCategoryForDetails();
public string CategoryName { get; set; } = "";
protected async override Task OnInitializedAsync()
{
GetSubCategories = (await subcategoryService.GetSubCategories()).ToList();
CategoryName = (await categoryService.GetCategory(subcategory.CategoryId)).Name;
}
AllowPaging="true"
AllowSorting="true"
AllowResizing="true"
AllowReordering="true"
AllowFiltering="true">
HeaderText="ID #" TextAlign="@TextAlign.Right"
Width="20">
HeaderText="Parent Category" TextAlign="@TextAlign.Left"
Width="80">
HeaderText="Name" TextAlign="@TextAlign.Left"
Width="80">
HeaderText="Description" TextAlign="@TextAlign.Left"
Width="80">

2 Replies 1 reply marked as answer

PA Paul January 23, 2021 11:53 AM UTC

I have decided to use navigation property to obtain the Category Name. I thought I should let you know.

Thanks anyway for your usual assistance


RN Rahul Narayanasamy Syncfusion Team January 27, 2021 01:52 PM UTC

Hi Paul, 

Greetings from Syncfusion. 

We have validated your query and we might suspect that you are facing difficulties while showing the foreign key values in Grid. We already have a default foreign key column support for this. Please find the below documentation for your reference. 

Reference: 

If we misunderstood your requirement, then could you please share below details. It will be helpful to validate and provide a better solution. 

  • Full Grid code snippets.
  • Share a simple reproduceable sample if possible.

Regards, 
Rahul 


Marked as answer
Loader.
Up arrow icon