Hi, i made a grid having multiple field taken from the same foreign table (same foreign key), when i try grid sorting clicking on the table header, whatever is the field i click on, it only sorts the first column which has the foreign key as field. Not only that, sorting is also wrong because is based on the foreign key value and not on the foreign field i'm showing.
Here's my .cshtml grid
@(
Html.EJS().Grid<Note>
("Note").DefaultGrid().AllowSorting(true)
.Columns(col =>
{
col.Field(x => x.Id).IsPrimaryKey(true).Visible(false).Add();
col.Field("Customer_Id").ForeignKeyField("Id").ForeignKeyValue("BirthDay").DataSource((IEnumerable<Customer>)ViewBag.customerList).HeaderText("Birthday").Type("date").AllowSorting(true).AllowFiltering(false).Width(140).AddDate();
col.Field("Customer_Id").ForeignKeyField("Id").ForeignKeyValue("Activity").DataSource((IEnumerable<Customer>)ViewBag.customerList).HeaderText("Workplace").Width(140).AddString();
col.Field("Customer_Id").ForeignKeyField("Id").ForeignKeyValue("Phone").DataSource((IEnumerable<Customer>)ViewBag.customerList).HeaderText("Phone").AllowFiltering(false).AllowSorting(true).Width(120).Add();
col.Field("CustomerStatus_Id").ForeignKeyField("Id").ForeignKeyValue("CustomerStatus_Id").DataSource((IEnumerable<Customer>)ViewBag.customerList).HeaderText("Status").Template("#customerStatusTemplate").Type("date").Width(200).AllowFiltering(true).Add();
col.Field(x => x.Description).HeaderText("Note").AddString();
}).Render()
If i click on Activity or Phone, it sorts the BirthDay column which is the first one having Customer_Id as field.
Thank you in advance.
Hi Marco,
Greetings from Syncfusion support
We have checked your shared sample and we found that you are using Duplicate columns with the same field name (Customer_Id). This was the cause of the problem.
By default, all the Grid actions are performed based on the column field name. So, the column field must be unique. Otherwise, all the actions will be affected only which is defined first in the column definition. So, we suggest you avoid the duplicate columns in the EJ2 Grid and achieve your requirement.
Regards,
Rajapandi R
Hi,
thank you for your reply.
I understood that grid uses Field name as identifier for its actions.
However, in my case, isn't Field required to bind the "main" dataSource foreign key (Customer_Id) to the second dataSource primary key (Id)? (as second dataSource i mean ViewBag.customerList)
And considering that i'm showing more than one field using customerList as dataSource, i have to put it as Field multiple times.
Am i doing something wrong that i don't get? Because other than the sorting problem the grid is working fine.
Hi Marco,
Thanks for the update
Since the column field is the key for Grid actions, it is essential to be the grid columns field to have unique values. So, there are no other options to have duplicate fields or perform sorting considering the data from a foreign key table rather than the field value.
Regards,
Rajapandi R