Articles in this section
Category / Section

Server-side API for DataManager operations with IQueryable data

1 min read

The DataManager class helps in binding the Grid queries passed to the server-side. Based on those queries you can perform server-side operation on the Grid data.

The query parameters that help you perform the server-side operations are as follows.

Table 1: Query Parameters

Expand

It is used as OData Expand query.

RequiresCounts

When this property is set as True, the total count of records are included in the result.

Skip

Details regarding current page are skipped.

Take

Used to take required records from data manager.

Sorted

Records return the sorted collection.

Table

It is a data source table name.

Where

Records return the filter collection.

DataManager Operations:

The query parameters are serialized by the DataManager class and the server-side operations such as sorting, filtering, paging are performed by the PerformSorting, PerfomWhereFilter, PerformSkip and PerformTake methods.

C#

  public ActionResult DataSource(DataManager data)
        {
            IQueryable<OrdersView> datasource = new NorthwindDataContext().OrdersViews;
            if (data.Where != null) // for filtering
                datasource = QueryableDataOperations.PerformWhereFilter(datasource, data.Where, data.Where[0].Condition);
            if (data.Sorted != null)//for sorting
                datasource = QueryableDataOperations.PerformSorting(datasource, data.Sorted);
            if (data.Search != null)
                datasource = QueryableDataOperations.PerformSearching(datasource, data.Search);
            int count = datasource.Count();
            if (data.Skip >= 0)//for paging
                datasource = QueryableDataOperations.PerformSkip(datasource, data.Skip);
            if (data.Take > 0)//for paging
                datasource = QueryableDataOperations.PerformTake(datasource, data.Take);
            return Json(new { result = datasource.ToList(), count = count }, JsonRequestBehavior.AllowGet);
        }

DataOperations Method:

You can also perform the server side operation such as filtering, paging and sorting by using the DataOperations Execute method instead of using the individual methods. The following code example shows you how to use the Execute method in the DataManager.

Table 2: Server side operation order for Execute method

Server side operation Orders

  1. PerformWhereFilter
  1. PerformSearching
  1. PerformSorting
  1. PerformSelect
  1. PerformSkip
  1. PerformTake

C#

  public ActionResult DataSource(DataManager data)
        {
 
IQueryable<OrdersView> datasource = new NorthwindDataContext().OrdersViews;
            var count = datasource.AsQueryable().Count();
            var dm = QueryableDataOperations.Execute(datasource, data);
            return Json(new { result = dm, count = count });
        }

MVC

In the following code example is demonstrated the server-side operation using UrlAdaptor.

C#

[Controller]
namespace SyncfusionMvcApplication13.Controllers
{
    public class GridController : Controller
    {
        //
        // GET: /Grid/     
        public ActionResult GridFeatures()
        {
            
            return View();
            
        }
        public ActionResult DataSource(DataManager data)
        {
IQueryable<OrdersView> datasource = new NorthwindDataContext().OrdersViews;
            var count = datasource.AsQueryable().Count();
            var dm = QueryableDataOperations.Execute(datasource, data);
            return Json(new { result = dm, count = count });        
         }
    }
}
 [View]
@(Html.EJ().Grid<OrdersView>("FlatGrid")
         .Datasource(ds => ds.URL("/Grid/DataSource").Adaptor("UrlAdaptor"))
         .AllowFiltering()
         .AllowSorting()    /*Sorting Enabled*/
         .AllowPaging()    /*Paging Enabled*/
         .Columns(col =>
        {
            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
            col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
            col.Field("EmployeeID").HeaderText("Employee ID").TextAlign(TextAlign.Right).Width(75).Add();           col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(75).Format("{0:C}").Add();
            col.Field("OrderDate").HeaderText("Order Date").TextAlign(TextAlign.Right).Width(80).Format("{0:MM/dd/yyyy}").Add();
            col.Field("ShipCity").HeaderText("Ship City").Width(110).Add();
        }))

ASP

In ASP.NET, the server-side operation is performed by using the WebMethodAdaptor, and the DataManager parameter is set as value.

C#

[Default.aspx]
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <div class="row">
        <div class="col-md-9" style="margin-top: 10px">
            <ej:Grid ID="EmployeesGrid" runat="server" Width="1500px" AllowPaging="true" AllowFiltering="true" AllowSorting="true">
                <DataManager URL="Default.aspx/Data" Adaptor="WebMethodAdaptor" />
                <Columns>
                        <ej:Column Field="OrderID" IsPrimaryKey="true" HeaderText="Order ID" Width="80" />
                        <ej:Column Field="EmployeeID" HeaderText="Employee ID" Width="80" />
                        <ej:Column Field="Freight" HeaderText="Freight" Width="100" EditType="Numeric" />
                        <ej:Column Field="ShipCity" HeaderText="ShipCity" Width="80" />
                    </Columns>
            </ej:Grid>
        </div>
    </div>
</asp:Content>
[Default.aspx.cs]
public partial class _Default : Page
    {   
        protected void Page_Load(object sender, EventArgs e)
        {
                    }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static object Data(Syncfusion.JavaScript.DataManager value)
        {
IQueryable<OrdersView> datasource = new NorthwindDataContext().OrdersViews;
            var count = datasource.AsQueryable().Count();
            var dm = QueryableDataOperations.Execute(datasource, value);
            return new { result = dm, count = count };        }      
        }

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied