ForeignKeyValue Template Column

Hi,

I want to use multiple columns for my foreign key. 

For example, in te below Scenario, I want to use ProjectNo + ProjectRevision

<GridForeignColumn Field=@nameof(Stock.MaterialMasterId) HeaderText="Material" ForeignKeyValue="ProjectNo + ProjectRevision" ForeignKeyField="Id" ForeignDataSource="@Projects" Width="150">

                            <EditTemplate>

                                <SfComboBox ID="ProjectId" TItem="Project" TValue="int?" @bind-Value="@((context as Stock).ProjectId)" DataSource="@Projects">

                                    <ComboBoxFieldSettings Text="ProjectNo" Value="Id"></ComboBoxFieldSettings>

                                </SfComboBox>

                            </EditTemplate>

                        </GridForeignColumn>

And also in the combo box, I want to load the same.

Is it possible?

Thanks



5 Replies

JP Jeevakanth Palaniappan Syncfusion Team August 26, 2021 10:09 AM UTC

Hi Hajir, 

Greetings from Syncfusion support. 

We have checked your query but we are quite unclear about your exact scenario since we don’t know the model class details. So please share us the below details which will be helpful for us to validate your query. 

  1. Provide more information on your query.
  2. Do you want to show the data of two field properties from the ForeignKey class?
  3. Share us the model class of grid and foreignkey model class.

Regards, 
Jeevakanth SP. 



HA Hajir August 26, 2021 10:44 AM UTC

Hi, Thank you for your reply. Sure.

I have a PML class which has a foreign key to Drawnig table as below:

public class PML : BaseEntity

    {

        public int Id {get; set;}

        public int? DrawingId { get; set; }

        public Drawing Drawing { get; set; }

        public string DocumentNo { get; set; }

    }


And in my Drawing Class I have multiple properties:

public class Drawing : BaseEntity

    {

        public string DrawingName { get; set; }        

        public string SheetNo { get; set; }        

        public string Revision { get; set; }

        public int? PMLId { get; set; }

        public PML PML { get; set; }

   }


This is my Blazor client side page:


 <SfGrid DataSource="Entities" Toolbar="@(new List<string>() {"Add", "Edit", "Delete", "Cancel", "Update"})" AllowSorting="true" AllowExcelExport="true" AllowPdfExport="true" AllowPaging="true" AllowFiltering="true" TValue="PML">

                    <GridEvents TValue="PML" OnActionBegin="OnBeginHandler" OnActionComplete="OnActionComplete" OnActionFailure="OnActionFailure"></GridEvents>

                    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings>

                    <GridColumns>

<GridForeignColumn Field=@nameof(PML.DrawingId) HeaderText="Drawing No" ForeignKeyValue="DrawingNo" ForeignKeyField="Id" ForeignDataSource="@Drawings" Width="150" ValidationRules="@(new ValidationRules{ Required=true})">

                            <EditTemplate>

                                <SfComboBox ID="DrawingId" TItem="Drawing" TValue="int?" @bind-Value="@((context as PML).DrawingId)" DataSource="@Drawings">

                                    <ComboBoxFieldSettings Text="DrawingNo" Value="Id"></ComboBoxFieldSettings>

                                </SfComboBox>

                            </EditTemplate>

                        </GridForeignColumn>

                         

                        <GridColumn Field=@nameof(PML.DocumentNo)></GridColumn>

                       <GridColumn Field=@nameof(PML.Id) IsPrimaryKey="true" IsIdentity="true" Visible="false"></GridColumn>                       

                    </GridColumns>

                </SfGrid>

@code {


    private List<Drawing> Drawings { get; set; }

    private List<Painting> Paintings { get; set; }


    protected override async Task OnInitializedAsync()

    {

        Drawings = await DrawingRepository.GetAll();

        Paintings = await PaintingRepository.GetAll();

        await base.OnInitializedAsync();

    }

}



Here  the user can see the DrawingNo through the foreign key PML.DrawingID and also select the desired DrawingNo from combo box which is great.

But since drawings have different sheets and revisions, I need to display DrawingNo + SheetNo + Revision (in a table or string) on both row and combo box.


I found this article which is great, but I want something similar for combobox and foreign key.


Thanks




JP Jeevakanth Palaniappan Syncfusion Team August 27, 2021 07:08 AM UTC

Hi Hajir, 
 
Thanks for sharing the details. 
 
We have prepared a sample based on your requirement in which we have used column template feature of grid and ItemTemplate of Combo box. Please refer the complete code snippet and documentation below for your reference. 
 
Documentation: 

@using Syncfusion.Blazor.Grids 
@using Syncfusion.Blazor.DropDowns 
 
<SfGrid DataSource="@Orders" Height="315" Toolbar="@(new List<string>() { "Add","Edit","Delete","Update","Cancel"})"> 
    <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) IsPrimaryKey="true" HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridForeignColumn Field=@nameof(Order.EmployeeID) HeaderText="Employee Name" ForeignKeyValue="FirstName" ForeignDataSource="@Employees" Width="150"> 
            <Template> 
                @{ 
                    var con = (context as Order); 
                    EmployeeData ForeignKeyData = Employees.Where(x => x.EmployeeID == con.EmployeeID).FirstOrDefault(); 
                    <span>@ForeignKeyData.FirstName @ForeignKeyData.LastName</span> 
                } 
            </Template> 
            <EditTemplate> 
                <SfComboBox ID="EmployeeID" TItem="EmployeeData" TValue="int?" @bind-Value="@((context as Order).EmployeeID)" DataSource="@Employees"> 
                    <ComboBoxTemplates Context="ChildContext" TItem="EmployeeData"> 
                        <ItemTemplate> 
                            @{ 
                                EmployeeData ForeignKeyData = Employees.Where(x => x.EmployeeID == ChildContext.EmployeeID).FirstOrDefault(); 
                                <span>@ForeignKeyData.FirstName @ForeignKeyData.LastName</span> 
                            } 
                        </ItemTemplate> 
                    </ComboBoxTemplates> 
                    <ComboBoxFieldSettings Value="EmployeeID" Text="FirstName"></ComboBoxFieldSettings> 
                </SfComboBox> 
            </EditTemplate> 
        </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> 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    public List<Order> Orders { get; set; } 
    public List<EmployeeData> Employees { get; set; } 
 
    protected override void OnInitialized() 
    { 
        Orders = Enumerable.Range(1, 75).Select(x => new Order() 
        { 
            OrderID = 1000 + x, 
            EmployeeID = x, 
            Freight = 2.1 * x, 
            OrderDate = DateTime.Now.AddDays(-x), 
        }).ToList(); 
 
        Employees = Enumerable.Range(1, 75).Select(x => new EmployeeData() 
        { 
            EmployeeID = x, 
            FirstName = (new string[] { "Nancy", "Andrew", "Janet", "Margaret", "Steven" })[new Random().Next(5)], 
            LastName = (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? EmployeeID { get; set; } 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
    } 
} 

Regards, 
Jeevakanth SP. 
 



HA Hajir September 12, 2021 02:37 AM UTC

Thank you so much for the reply.


Is it possible to show the text same as template when we go to edit mode? for example, my template shows Drawing No + Revision. When we go edit mode, I want the text to display Drawing No + Revision as well. 

Thanks.


<GridColumn Field="@nameof(PML.DrawingId)" HeaderText="Drawing" EditType="EditType.DropDownEdit">

                            <Template>

                                @{

                                    var con = (context as PML);

                                    if (con != null)

                                    {

                                        <span>@con.Drawing?.DrawingNo [email protected]?.Revision</span>

                                    }

                                }

                            </Template>

                            <EditTemplate>                             

                                <SfComboBox ID="DrawingId" TItem="Drawing" TValue="int?" @bind-Value="@((context as PML).DrawingId)" DataSource="@DropDownDrawings">

                                    <ComboBoxTemplates Context="ChildContext" TItem="Drawing">

                                        <ItemTemplate>

                                            @{

                                                <span>@ChildContext.DrawingNo [email protected]</span>

                                            }

                                        </ItemTemplate>

                                    </ComboBoxTemplates>

                                    <ComboBoxFieldSettings Text=" DrawingNo" Value="Id"></ComboBoxFieldSettings>

                                </SfComboBox>

                            </EditTemplate>

                        </GridColumn>



JP Jeevakanth Palaniappan Syncfusion Team September 13, 2021 01:19 PM UTC

Hi Hajir, 
 
We suspect that you want to customize the input shown in the Combo box while editing a record. If so we suggest you to achieve your scenario by using the DropDownList instead of Combo box. In DropDownList,  you can set the ValueTemplate to achieve your scenario. Please refer the below code snippet, documentation and the sample for your reference. 

Documentation: 

Sample : 

@using Syncfusion.Blazor.Grids 
@using Syncfusion.Blazor.DropDowns 
 
<SfGrid DataSource="@Orders" Height="315" Toolbar="@(new List<string>() { "Add","Edit","Delete","Update","Cancel"})"> 
    <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) IsPrimaryKey="true" HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridForeignColumn Field=@nameof(Order.EmployeeID) HeaderText="Employee Name" ForeignKeyValue="FirstName" ForeignDataSource="@Employees" Width="150"> 
            <Template> 
                @{ 
                    var con = (context as Order); 
                    EmployeeData ForeignKeyData = Employees.Where(x => x.EmployeeID == con.EmployeeID).FirstOrDefault(); 
                    <span>@ForeignKeyData.FirstName @ForeignKeyData.LastName</span> 
                } 
            </Template> 
            <EditTemplate> 
                <SfDropDownList ID="EmployeeID" TItem="EmployeeData" TValue="int?" @bind-Value="@((context as Order).EmployeeID)" DataSource="@Employees"> 
                    <DropDownListTemplates Context="ChildContext" TItem="EmployeeData"> 
                        <ItemTemplate> 
                            @{ 
                                EmployeeData ForeignKeyData = Employees.Where(x => x.EmployeeID == ChildContext.EmployeeID).FirstOrDefault(); 
                                <span>@ForeignKeyData.FirstName @ForeignKeyData.LastName</span> 
                            } 
                        </ItemTemplate> 
                        <ValueTemplate> 
                            @{ 
                                EmployeeData ForeignKeyData = Employees.Where(x => x.EmployeeID == ChildContext.EmployeeID).FirstOrDefault(); 
                                <span>@ForeignKeyData.FirstName @ForeignKeyData.LastName</span> 
                            } 
                        </ValueTemplate> 
                    </DropDownListTemplates> 
                    <DropDownListFieldSettings Value="EmployeeID" Text="FirstName"></DropDownListFieldSettings> 
                </SfDropDownList> 
            </EditTemplate> 
        </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> 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    public List<Order> Orders { get; set; } 
    public List<EmployeeData> Employees { get; set; } 
 
    protected override void OnInitialized() 
    { 
        Orders = Enumerable.Range(1, 75).Select(x => new Order() 
        { 
            OrderID = 1000 + x, 
            EmployeeID = x, 
            Freight = 2.1 * x, 
            OrderDate = DateTime.Now.AddDays(-x), 
        }).ToList(); 
 
        Employees = Enumerable.Range(1, 75).Select(x => new EmployeeData() 
        { 
            EmployeeID = x, 
            FirstName = (new string[] { "Nancy", "Andrew", "Janet", "Margaret", "Steven" })[new Random().Next(5)], 
            LastName = (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? EmployeeID { get; set; } 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
    } 
} 


Get back to us if you have any other queries. 

Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon