Datagrid and One to Many relationship

Hello Radzen Community,

I have a datagrid filled with Entity Framwork Core.but I can't find a tutorial to fill columns with one to many relationship.

The column have to display the entity of this element defined like this :

public int? SectionElementPositionId { get; set; }

public virtual SectionBasicFunction? SectionBasicFunction { get; set; }

when I try to recover the SectionBasicFunction Name the grid can't display it....

Does we have a tutorial to set up a one to many realtionship with entity framework in the datagrid ?

Hi @jmarmouzet,

I don't understand your question. What are you trying to achieve? What is the expected result?

Hi Korchev,

So I have a datagrid :

This datagrid is filled with Entity framework...
But the One to many relationship doesn't work
I try to use the tutorial witth Blazor DataGrid binding dynamic data

The data is a List of Element like that

public partial class PredefinedElement
    {
        public PredefinedElement()
        {
            
        }
 public int? SectionElementPositionId { get; set; }
 public virtual SectionBasicFunction? SectionBasicFunction { get; set; }

And I want to display the "Section basic function" name corresponding

public SectionBasicFunction()
  {
      PredefinedElements = new HashSet<PredefinedElement>();
  }
public int SectionBasicFunctionId { get; set; }
public string? Name { get; set; }

public virtual ICollection<PredefinedElement> PredefinedElements { get; set; }

do you have a solution to this ?

What doesn't work? What do you want to happen? Please explain the desired outcome or we won't be able to help.

Ok So
I want this column to be filed with the Name of the "Section Basic Function" which is selected from another table with a one to many relationship

You will need to set the Template of the column then. The related entity has to be included in the result set. You can check this demo which shows how to do that:

        <RadzenDataGridColumn TItem="Order" Property="Employee.LastName" Title="Employee">
            <Template Context="order">
                @order.Employee?.FirstName @order.Employee?.LastName
            </Template>
        </RadzenDataGridColumn>

Here the related object is available via the Employee property. Don't forget to populate it in the query via Include():

dbContext.Orders.Include("Employee")

I tryed this but it still doesn't work

<RadzenDataGridColumn TItem="PredefinedElements.Shared.DBModels.PredefinedElement" Property="SectionBasicFunction.Name"  Title="Element Positon"  Width="140px"> 
        <Template Context="predefinedElement">
            @predefinedElement.SectionBasicFunction?.Name
        </Template>
</RadzenDataGridColumn>

with the controller like this

 var predefinedElements = await _dbContext.PredefinedElements
  .Include(p=>p.Products)
  .Include(p=>p.Markets)
  .Include(p=>p.ActuationTypes)
  .IncludeOptimized(p => p.SectionElementPosition)
  .IncludeOptimized(p => p.SectionElementType)
  .IncludeOptimized(p => p.SectionBasicFunction)
  .IncludeOptimized(p => p.SectionMouvmeentNaming)
  .IncludeOptimized(p => p.SectionCustomerPiping)
  .IncludeOptimized(p => p.SliceType)
  .IncludeOptimized(p => p.Efatemplate)
  .IncludeOptimized(p => p.EfaAgzTemplate)
  .IncludeOptimized(p => p.EfaAsmTemplate)
  .IncludeOptimized(p => p.EfaGezTemplate)
  .IncludeOptimized(p => p.FlangeFaceInCavity)
  .IncludeOptimized(p => p.FlangeFaceOut1Cavity)
  .IncludeOptimized(p => p.RequiredFlangeFaceCavity1)
  .IncludeOptimized(p => p.RequiredFlangeFaceCavity2)
  .IncludeOptimized(p => p.RequiredFlangeFaceCavity3)
  .IncludeOptimized(p => p.RequiredFlangeFaceCavity4)
  .IncludeOptimized(p => p.RequiredFlangeFaceCavity5)
  .IncludeOptimized(p => p.RequiredFlangeFaceCavity6)
  .IncludeOptimized(p => p.RequiredFlangeFaceCavity7)

  .ToListAsync();

What does that mean - is there compilation error, exception or something else? Is the SectionBasicFunction populated (you can check that with the debugger)?

There are no exception but the Name is not displayed again...

@predefinedElement.SectionBasicFunction

Is completed...so it should be displayed

Probably SectionBasicFunction is null and that's why it doesn't display. Check it with the debugger.

Hi Korchev,

I checked SectionBasicFunction is not null so it should be displayed

Yes it should. Can you prepare a sample runnable snippet that shows the problem? You can use in-memory data (List) as a data source.

Ok so I just copy/past some class used to run the code here in preformated text ??

We need a self-contained razor file which can be run without external dependencies or DB requirement.

Hi Korchev,

I rechecked the code, and I found a mistake, the solution you provide works correctly

THANKS For the help

Glad I was able to help!

1 Like