Foreign column and calculated column

Hello,

I want to ask if it is possible to have a calculated column that is based on column from a foreign data source?

For example,

if I have columns 
"price", "qty" on main data source
and 
"rate" on foreign data source,

then, am I able to create a column that is "price * qty * rate" using the build-in grid column template?

The problem now I have is the "context" object used in the calculated-column-template is always the model of the main data source. 
Is it possible to get an context object from the foreign data source as well?

If that is not possible, then i will need to create my own structure which binds the two source before binding to the grid i believe?

Thanks for your time in advance.



1 Reply 1 reply marked as answer

VN Vignesh Natarajan Syncfusion Team March 8, 2021 05:54 AM UTC

Hi Nick,  
 
Thanks for contacting Syncfusion support.  
 
Query: “I want to ask if it is possible to have a calculated column that is based on column from a foreign data source? 
 
Yes, we can achieve your requirement using ColumnTemplate feature of the Grid. But we will not be able to send the foreignKey datasource instance in the Template context.  Inside the Cell Template, we need to match the ForeignKeyField value from Grid datasource and ForeignKey datasource and find that particular record. From that object we can find the Rate value.  
 
Refer the below code example.  
 
<SfGrid DataSource="@Orders" Height="315"> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridForeignColumn Field=@nameof(Order.EmployeeID) HeaderText="Employee Name" ForeignKeyField="ProductID" ForeignKeyValue="ProductName" ForeignDataSource="@Employees" Width="150"></GridForeignColumn> 
        <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="Total" Format="C2" TextAlign="TextAlign.Right" Width="120"> 
            <Template> 
                @{ 
                    var employee = (context as Order); 
                    var foreignRate = Employees.Where(x => x.ProductID == employee.EmployeeID).FirstOrDefault().Rate; 
                    <div>@(employee.Freight * foreignRate * employee.Qty )</div> 
                } 
            </Template> 
        </GridColumn> 
    </GridColumns> 
</SfGrid> 
  
For your convenience we have prepared a sample which can be downloaded from below  
 
 
Please get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan  


Marked as answer
Loader.
Up arrow icon