Different Field Key name and Foreignfield key name in GridForeignColumn

Hi team

Is it possible  to map Field key to a different foreign field key name,

here is my example.

   
                                           EditType="EditType.DefaultEdit"
                                           HeaderText="Salesman"
                                           ForeignKeyValue="EmployeeName"
                                           ForeignDataSource="@Salesmen"
                                           Width="150">
     


The problem is that Foreign field key name is EmployeeID... different from salesmanID

I tried to give an alias in the code block, But I'm in no luck.. It returns error "list cannot be converted into Ienumerable"


  [Inject]
  public ICustomerService CustomerService { get; set; }

  [Inject]
  public IEmployeeService EmployeeService { get; set; }

   public IEnumerable Customers { get; set; }
   public IEnumerable Salesmen { get; set; }


 protected override async Task OnInitializedAsync()
        {
           Customers = (await CustomerService.GetCustomers()).OrderBy(e => e.CustomerName).ToList();

           Salesmen = (IEnumerable)(await EmployeeService.GetEmployees())
                            .Select(e => new EmployeeData { SalesmanID = e.EmployeeID, SalesmanName = e.EmployeeName }).ToList();
         }

Hope someone could help me on this matter.. thanks in advance



3 Replies 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team June 2, 2021 07:46 AM UTC

Hi Tyrone, 

Greetings from Syncfusion. 

Query: Different Field Key name and Foreignfield key name in GridForeignColumn 

We have validated your query and you want to bind the foreign key column when there is same mapping Field is not available in Foreign key datasource model class. If you don’t have same mapping field in your Grid datasource and foreign key datasource, then you can map both datasource by using ForeignKeyField property. 

ForeignDataSource - Defines the foreign data.  
ForeignKeyField - Defines the mapping column name to the foreign data.  
ForeignKeyValue - Defines the display field from the foreign data. 

Find the below code snippets for your reference. 

 
<SfGrid @ref="Grid" DataSource="@Orders" Height="315" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })"> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridForeignColumn Field=@nameof(Order.EmployeeID) HeaderText="Employee Name" ForeignKeyField="ID" ForeignKeyValue="FirstName" ForeignDataSource="@Employees" Width="150"></GridForeignColumn> 
        . . . 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    SfGrid<Order> Grid; 
    public List<Order> Orders { get; set; } 
    public List<EmployeeData> Employees { get; set; } 
 
    protected override void OnInitialized() 
    { 
        Orders = Enumerable.Range(1, 5).Select(x => new Order() 
        { 
            OrderID = 1000 + x, 
            EmployeeID = x, 
            Freight = 2.1 * x, 
            OrderDate = DateTime.Now.AddDays(-x), 
        }).ToList(); 
 
        Employees = Enumerable.Range(1, 5).Select(x => new EmployeeData() 
        { 
            ID = x, 
            FirstName = (new string[] { "Nancy", "Andrew", "Janet", "Margaret", "Steven" })[new Random().Next(5)], 
        }).ToList(); 
    } 
 
    public class Order 
    { 
        public int? OrderID { get; set; } 
        public int? EmployeeID { get; set; } 
        public DateTime? OrderDate { get; set; } 
        public double? Freight { get; set; } 
    } 
 
    public class EmployeeData 
    { 
        public int? ID { get; set; } 
        public string FirstName { get; set; } 
    } 
} 


Reference: 

Please let us know if you have any concerns. 

Regards, 
Rahul 


Marked as answer

TY Tyrone June 3, 2021 05:27 AM UTC

Thanks Raul. It worked now.. 


RN Rahul Narayanasamy Syncfusion Team June 4, 2021 05:40 AM UTC

Hi Tyrone, 
 
Thanks for the update. 
 
We are happy to hear that the provided solution was helpful to achieve your requirement. Please get back to us if you need further assistance. 
 
Regards, 
Rahul 


Loader.
Up arrow icon