DataTable and DataManager

Hi!

I am using a DataTable as the datasource for the grid (because I have dynamic columns).

But I need to do pagination/filtering on the server side, in the MVC controller. And it seems that I don't know how to use the DataManager together with the UrlAdapter (to get the pagination info)

Here is my code:

View:
  @(Html.EJ().Grid<object>("ResourcesGrid")
          .Datasource(ds => ds.URL(Url.Action("Resources")))
          .AllowFiltering()
          .AllowSorting()
          .AllowPaging()
          .AllowResizeToFit()
          .IsResponsive()
          // columns not declared
          )

Controller:
    public ActionResult Resources(DataManager dm)
        {
            var dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Age");

            DataRow row1 = dt.NewRow();
            row1["Id"] = 1;
            row1["Name"] = "Catalin";
            row1["Age"] = 10;

            DataRow row2 = dt.NewRow();
            row2["Id"] = 2;
            row2["Name"] = "Dero";
            row2["Age"] = 15;


            dt.Rows.Add(row1);
            dt.Rows.Add(row2);

            if (dm.Sorted != null && dm.Sorted.Count > 0)
            {
                // Sorting
            }

            if (dm.Where != null && dm.Where.Count > 0) //Filtering
            {
            }

            int count = 0;// after sorting and filtering => Count

            if (dm.Skip != 0)
            {
              
            }
            if (dm.Take != 0)
            {
              
            }

            return this.Json(new { result = dt, count }, JsonRequestBehavior.AllowGet);
        }

5 Replies

SE Sathyanarayanamoorthy Eswararao Syncfusion Team February 8, 2018 12:56 PM UTC

Hi Catalin, 

Thanks for contacting Syncfusion support. 

We have analyzed your query and found that you need to perform Paging and Filtering operations on the Server side. We already have a Knowledge Base documentation for this query. 

Please refer the below KB documentation for details of Datamanager operations on server side. 


Regards, 
Sathyanarayanamoorthy 



CR Catalin Radoi February 8, 2018 02:39 PM UTC

I know how to do pagination and all that, problem is I cannot use both datatable as a source and URL  (To have the pagination info)!!!!!!!!!


I need to use a datatable as a datasource, because I have dynamic columns

but also I need to use DataSource(ds => ds.Url()) to have the pagination info, for the server side.

But they don't seem to work together or I dunno :(


SE Sathyanarayanamoorthy Eswararao Syncfusion Team February 9, 2018 01:29 PM UTC

Hi Catalin, 

To perform server side paging operation with datatable we suggest you to convert the dataTable to JSON.  So, we suggest you to use the DataTableToJson() method for converting DataTable to IEnumerable Dictionary collection in server-side. In this method we need to pass the DataTable as a parameter and it will return the data.  

Find the code example and sample:  


@(Html.EJ().Grid<object>("MultiSort") 
    .Datasource(ds => ds.URL("/Grid/DataSource").Adaptor(AdaptorType.UrlAdaptor)) 
    .AllowFiltering() 
    .AllowSorting() 
    .AllowPaging() 
    .PageSettings(page => page.PageSize(1)) 
    .AllowResizeToFit() 
    .IsResponsive() 
) 
 
------------------------------------------- 
 
public ActionResult GridFeatures() 
        { 
            var dt = new DataTable(); 
            dt.Columns.Add("Id"); 
            dt.Columns.Add("Name"); 
            dt.Columns.Add("Age"); 
 
            DataRow row1 = dt.NewRow(); 
            row1["Id"] = 1; 
            row1["Name"] = "Catalin"; 
            row1["Age"] = 10; 
 
            DataRow row2 = dt.NewRow(); 
            row2["Id"] = 2; 
            row2["Name"] = "Dero"; 
            row2["Age"] = 15; 
 
            dt.Rows.Add(row1); 
            dt.Rows.Add(row2); 
            HttpContext.Session["Table"] = dt; 
            return View(); 
        } 
 
public ActionResult DataSource(DataManager dm) 
        { 
            var DataSource = (DataTable)HttpContext.Session["Table"]; 
 
            IEnumerable Data = Syncfusion.JavaScript.Utils.DataTableToJson(DataSource); 
 
            Syncfusion.JavaScript.DataSources.DataOperations operation = new Syncfusion.JavaScript.DataSources.DataOperations(); 
            ---------------------------- 
           int count = Data.AsQueryable().Count(); 
            if (dm.Skip != 0) 
            { 
                Data = operation.PerformSkip(Data, dm.Skip); 
            } 
            if (dm.Take != 0) 
            { 
                Data = operation.PerformTake(Data, dm.Take); 
            } 
            return Json(new { result = Data, count = count }, JsonRequestBehavior.AllowGet); 
        } 


Regards, 
Prasanna Kumar N.S.V 



CR Catalin Radoi February 14, 2018 12:40 PM UTC

It works like this.
Thanks!


SE Sathyanarayanamoorthy Eswararao Syncfusion Team February 15, 2018 03:39 AM UTC

Hi Catalin, 

We are happy to hear that your issue has been solved. 
If you need any further assistance please get back to us. 

Regards, 
Sathyanarayanamoorthy 


Loader.
Up arrow icon