Odatav4 and One to many

Hi,

I have db with two tables - Orders and Persons - Orders have OrderID and OrderNumber, and Persons have ID, FirstName, LastName, OrderID. I made end point for the two tables as https://example.com/Persons, and https://example.com/Orders. I have also https://example.com/Persons?$expand=Orders

I want to show all the orders in a grid - with colums OrderdID, OrderNumber, Person.FirstName, Person.LastName, but i cant make it to work. Can you give example? I treied every possible combination.

4 Replies

RN Rahul Narayanasamy Syncfusion Team April 17, 2020 01:10 PM UTC

Hi Stefan, 

Greetings from Syncfusion. 

We have validated your query with the provided information and we suspect that you want to bind Complex data to one of the columns of the Grid. Refer the below documentation for your reference. 


If it does not meet your requirement or if we misunderstood, then please get back to us with more details regarding your requirement. 

Regards, 
Rahul 



ST Stefan April 17, 2020 05:13 PM UTC

Yes that is what i followed. Even the remark - For OData and ODataV4 adaptors, you need to add expand query to the query property (of DataGrid), to eager load the complex data

It renders, but then it show only one of each record - every Person, but with the collumn OrderID show as empty. And some Persons have 2-3 orders. What is wrong in this example:

<SfGrid TValue="Person" AllowPaging="true">
    <SfDataManager Url="test.com/Persons?$expand=Orders" Adaptor="Adaptors.ODataV4Adaptor"></SfDataManager>
    <GridColumns>
        <GridColumn Field=@nameof(Person.ID) HeaderText="Person ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
        <GridColumn Field=@nameof(Person.FirstName) HeaderText="First Name" TextAlign="TextAlign.Right" Width="120"></GridColumn>
        <GridColumn Field="Order.OrderID" HeaderText="Order Number" TextAlign="TextAlign.Right" Width="120"></GridColumn>
    </GridColumns>
</SfGrid>

 public class Orders
        {
            public int? OrderID { get; set; }
            public int OrderNumber { get; set; }
            public string PersonID { get; set; }
        }

        public class Person
        {
            public string FirstName { get; set; }
            public int? ID { get; set; }
            public Orders Order {get; set;}


RN Rahul Narayanasamy Syncfusion Team April 24, 2020 03:14 AM UTC

Hi Stefan, 

Thanks for sharing the details.  

We are able to reproduce the problem with the shared data. Currently we are validating the reported problem at our end and we will update the further details on April 24, 2020. Until then we appreciate your patience. 

Regards, 
Rahul 



RN Rahul Narayanasamy Syncfusion Team April 28, 2020 12:14 PM UTC

Hi Stefan,
  
Sorry for the delay in get back to you. 

We have validated your query with provided details at our end. Yes, we can display the Complex data(like your data) by using Template property. Here, we have created a sample based on your data and displayed the complex data in Product ID column. Find the below code snippets and sample for your reference. 

 
<SfGrid TValue="Order" AllowPaging="true"> 
    <SfDataManager Url="https://services.odata.org/V4/Northwind/Northwind.svc/Orders/?$expand=Order_Details&$format=json" Adaptor="Adaptors.ODataV4Adaptor"></SfDataManager> 
    <GridColumns> 
 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn> 
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn HeaderText="Product ID" Width="150"> 
            <Template> 
                @{ 
                    var person = (context as Order); 
                    @foreach (Order_Details item in @person.order_Details) 
                    { 
                        <div>@item.ProductID </div> 
                    } 
                } 
            </Template> 
        </GridColumn> 
    </GridColumns> 
</SfGrid> 
 
@code{ public Query Qry = new Query().Expand(new List<string>() { "Orders" }); 
 
    public class Order 
    { 
        public int? OrderID { get; set; } 
        public string CustomerID { get; set; } 
        public DateTime? OrderDate { get; set; } 
        public double? Freight { get; set; } 
        public List<Order_Details> order_Details { get; set; } 
    } 
 
    public class Order_Details 
    { 
        public int? OrderID { get; set; } 
        public int? ProductID { get; set; } 
        public double? UnitPrice { get; set; } 
        public double? Quantity { get; set; } 
    } 
 
    } 

Note: We have used our ODataV4 service link for data binding. Since the provided link is not in usable state(provided link is  not in active).  

If you have faced difficulties in binding the data, then could you please active your dataSource link and get back to us. 

Regards, 
Rahul  


Loader.
Up arrow icon