- Home
- Forum
- ASP.NET MVC
- Foreign Key Column in Grid - CRUD operations and Sorting
Foreign Key Column in Grid - CRUD operations and Sorting
Hi!




I have a grid with three first fields as Foreogn Key Columns and the last one it's a normal string field:
I want to make CRUD operations and sorting this grid but I cannot find the way to do that. I tried to combine these information but without success:
https://help.syncfusion.com/aspnetmvc/grid/columns?cs-save-lang=1&cs-lang=razor#foreign-key-column
https://help.syncfusion.com/aspnetmvc/grid/data-adaptors?cs-save-lang=1&cs-lang=js#foreign-key-adaptor
Is there a way to make CRUD operations and sorting in the same grid?
This is my view:
This is part of my code:
And CRUD operations:
Thanks, regards!
SIGN IN To post a reply.
3 Replies
RS
Renjith Singh Rajendran
Syncfusion Team
October 13, 2017 01:57 PM UTC
Hi Daniel,
We have analyzed your query and suspect that you want to sort a foreignkey column based on value and not field name. We have achieved your requirement by sorting the foreingkey dataSource (which bound to the column), and based on that collected the records from the Grid’s dataSource pushed to new set of records and bind them to Grid. Please refer to the following code example.
cshtml:
|
@(Html.EJ().Grid<OrdersView>("Grid")
.Datasource(ds => ds.URL("UrlDataSource").UpdateURL("UrlUpdate").InsertURL("UrlInsert").RemoveURL("UrlDelete")
.Adaptor(AdaptorType.UrlAdaptor))
.AllowPaging()
.AllowSorting()
.Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(90).Add();
col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
col.Field("EmployeeID").HeaderText("Employee Name").ForeignKeyField("EmployeeID")
.ForeignKeyValue("FirstName").DataSource((IEnumerable<object>)ViewBag.dataSource2)
.TextAlign(TextAlign.Left).Width(90).Add();
})
) |
cs:
|
public ActionResult DataSource(DataManager dm)
{
. . ..
. . . .
List<Orders> ord = (List<Orders>)order;
DataOperations dp = new DataOperations();
if (dm.Sorted != null)
{
if (dm.Sorted[0].Name == "EmployeeID")
order = SortFor(ord, emp, dm.Sorted);//Only for foreignKey Column
else
order = dp.PerformSorting(order, dm.Sorted);
}
var count = order.AsQueryable().Count();
if (dm.Skip != null && dm.Take != null)
{
order = dp.PerformSkip(order, dm.Skip);
order = dp.PerformTake(order, dm.Take);
}
return new { result = order, count = count };
}
public List<Orders> SortFor(List<Orders> ord, List<Employee> emp, List<Sort> Sorted)
{
if (Sorted[0].Direction == "ascending")//check whether ascending or descending
emp = emp.OrderBy(e => e.FirstName).ToList();
else
emp = emp.OrderByDescending(e => e.FirstName).ToList();
List<Orders> or = new List<Orders>();
for (int i = 0; i < emp.Count(); i++)
{ //Select the Field matching records
IEnumerable<Orders> list = ord.Where(pred => pred.EmployeeID == emp[i].EmployeeID).ToList();
or.AddRange(list);
}
return or;
} |
Note: The solution will work only for the single sorting and not the multi sorting.
Regards,
Renjith Singh Rajendran.
DA
Daniel
October 13, 2017 10:11 PM UTC
Hi! All works fine,
Thanks for your help,
Regards!
PK
Prasanna Kumar Viswanathan
Syncfusion Team
October 16, 2017 05:08 AM UTC
Hi Daniel,
We are happy to hear that your issue has been resolved.
Please let us know if you need any further assistance.
Regards,
Prasanna Kumar N.S.V
SIGN IN To post a reply.
- 3 Replies
- 3 Participants
-
DA Daniel
- Oct 12, 2017 10:08 PM UTC
- Oct 16, 2017 05:08 AM UTC