Summary Column

Hi,
I am having some trouble getting my summary column to work.  All I ever see is a 0.

Here is my grid declaration:
    @(Html.EJ().Grid<object>("MasterGrid")
                    .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
                    {
                        items.AddTool(ToolBarItems.ExcelExport);
                    }))
                    .Mappers(map => map.ExportToExcelAction("Reports/ExportMasterGrid"))
                   .ClientSideEvents(events => events.Load("onLoad").DataBound("dataBound"))
                   .Datasource(ds => ds.URL(@Url.Action("DataSource"))
                   .Adaptor(AdaptorType.UrlAdaptor))
                   .AllowPaging()
                   .AllowSorting()
                   .AllowFiltering()
                   .EnableAltRow()
                   .ShowSummary()
    .SummaryRow(row =>
    {
        row.Title("Count").SummaryColumns(col => { col.SummaryType(SummaryType.Sum).DisplayColumn("LeadCount").DataMember("LeadCount").Add(); }).Add();
    })
        .Columns(col =>
        {
            col.Field("State").HeaderText("State").Add();
            col.Field("MailDate").HeaderText("Mail Date").Format("{0:M/d/yyyy}").Add();
            col.Field("LeadCount").HeaderText("Lead Count").Add();


        })
        .ClientSideEvents(eve => { eve.RowSelected("rowSelected"); })
    )

Here is my c# code:
 public ActionResult DataSource(Syncfusion.JavaScript.DataManager dm)
        {
            int count = 0;
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            string JSONString = null;
            using (SqlConnection cn = new SqlConnection(connStr))
            {

                cn.Open();

                //Getting the records based on skip and top value. 
                SqlCommand cmd = new SqlCommand("GetLeadsByState", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                dt = (DataTable)ds.Tables[0];

                JSONString = JsonConvert.SerializeObject(dt, new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    Formatting = Formatting.Indented
                });

            }

            IEnumerable Data = (from DataRow row in dt.Rows
                                select new DKDatabaseModelClass.GrossLeadsByStateModel
                                {
                                    State = row["State"].ToString(),
                                    MailDate = Convert.ToDateTime(row["MailDate"]),
                                    LeadCount = (int)row["LeadCount"]
                                }).ToList();


            Syncfusion.JavaScript.DataSources.DataOperations operation = new Syncfusion.JavaScript.DataSources.DataOperations();
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting 
            {
                Data = operation.PerformSorting(Data, dm.Sorted);
            }
            if (dm.Where != null && dm.Where.Count > 0) //Filtering 
            {
                Data = operation.PerformWhereFilter(Data, dm.Where, dm.Where[0].Operator);
            }
            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);

        }

Thanks for your help.

5 Replies

SA Saravanan Arunachalam Syncfusion Team November 3, 2017 07:30 AM UTC

Hi Richard, 
Thanks for contacting Syncfusion’s support. 
We have analyzed your query, the cause of the issue is that you have not handle the aggregation on server side and we have already discussed this query in the following UG documentation link. 
Regards, 
Saravanan A. 



RD Richard Dublon November 16, 2017 03:19 PM UTC

Hi,
I was able to get the summary column to work but it doesn't update when I filter the grid.
Here is my view code:
 @(Html.EJ().Grid<object>("StateGrid")
                    .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
                    {
                        items.AddTool(ToolBarItems.ExcelExport);
                    }))
                    .Mappers(map => map.ExportToExcelAction("ExportStateGrid"))
                   .ClientSideEvents(events => events.Load("onLoad").DataBound("dataBound"))
                   .Datasource(ds => ds.URL(@Url.Action("LeadsByState"))
                   .Adaptor(AdaptorType.UrlAdaptor))
                   .AllowPaging()
                   .AllowSorting()
                   .AllowFiltering()
                   .FilterSettings(filter => { filter.FilterType(FilterType.Excel); })
                   .EnableAltRow()
                   .ShowSummary()
    .SummaryRow(row =>
    {
        row.Title("Count").SummaryColumns(col => { col.SummaryType(SummaryType.Sum).DisplayColumn("LeadCount").DataMember("LeadCount").Add(); }).Add();
    })
        .Columns(col =>
        {
            col.Field("State").HeaderText("State").Add();
            col.Field("MailDate").HeaderText("Mail Date").Format("{0:M/d/yyyy}").Add();
            col.Field("LeadCount").HeaderText("Lead Count").Add();


        })
        .ClientSideEvents(eve => { eve.RowSelected("rowSelected"); })
    )

Here is my controller code:
            DataResult result = new DataResult();
            DataTable dtResult = new DataTable();
            int count = 0;
            dtResult = GetGridData("spGetLeadsByState");

            IEnumerable Data = (from DataRow row in dtResult.Rows
                                select new DKDatabaseModelClass.GrossLeadsByStateModel
                                {
                                    State = row["State"].ToString(),
                                    MailDate = Convert.ToDateTime(row["MailDate"]),
                                    LeadCount = (int)row["LeadCount"]
                                }).ToList();

            Syncfusion.JavaScript.DataSources.DataOperations operation = new Syncfusion.JavaScript.DataSources.DataOperations();
            List<string> aggregateFields = new List<string>();
            if (dm.Aggregates != null)
            {
                for (var i = 0; i < dm.Aggregates.Count; i++)
                    aggregateFields.Add(dm.Aggregates[i].Field);
                result.aggregate = operation.PerformSelect(Data, aggregateFields);
            }

            if (dm.Search != null)
                Data = operation.PerformSearching(Data, dm.Search);
            if (dm.Where != null)
                Data = operation.PerformWhereFilter(Data, dm.Where, dm.Where[0].Condition);
            result.count = Data.AsQueryable().Count();
            if (dm.Skip != null && dm.Skip != 0)//skiped while rendering checkbox
                Data = operation.PerformSkip(Data, dm.Skip);
            if (dm.Take != null && dm.Take != 0)//skiped while rendering checkbox
                Data = operation.PerformTake(Data, dm.Take);
            result.result = Data;//passed the whole dataSource directly for checkbox rendering
            return Json(result, JsonRequestBehavior.AllowGet);



MS Mani Sankar Durai Syncfusion Team November 17, 2017 11:43 AM UTC

Hi Richard, 

We have checked your code example and we are able to reproduce the reported issue. Since you are performing server side filtering operation after the aggregation. So while handling aggregation before filtering it will retrieve the result based on all data. To avoid the issue we suggest you to handling aggregation based on filtered record instead of whole data (set the aggregation code after performing filtering). 
Please refer the code example 
  public ActionResult UrlDataSource(DataManager dm) 
        { 
 
            IEnumerable Data = new NorthwindDataContext().OrdersViews.ToList(); 
            DataResult result = new DataResult(); 
            Syncfusion.JavaScript.DataSources.DataOperations operation = new Syncfusion.JavaScript.DataSources.DataOperations(); 
            List<string> aggregateFields = new List<string>(); 
            if (dm.Search != null) 
                Data = operation.PerformSearching(Data, dm.Search); 
            if (dm.Where != null) 
                Data = operation.PerformWhereFilter(Data, dm.Where, dm.Where[0].Condition);       //perform filtering  
 
            if (dm.Aggregates != null) 
            { 
                for (var i = 0; i < dm.Aggregates.Count; i++) 
                    aggregateFields.Add(dm.Aggregates[i].Field); 
                result.aggregate = operation.PerformSelect(Data, aggregateFields); 
            }           //perform aggregation. 
            result.count = Data.AsQueryable().Count(); 
          
            if (dm.Skip != null && dm.Skip != 0)//skiped while rendering checkbox 
                Data = operation.PerformSkip(Data, dm.Skip); 
            if (dm.Take != null && dm.Take != 0)//skiped while rendering checkbox 
                Data = operation.PerformTake(Data, dm.Take); 
            result.result = Data;//passed the whole dataSource directly for checkbox rendering 
            return Json(result, JsonRequestBehavior.AllowGet); 
        } 
 

Refer the online sample link 

Please let us know if you need further assistance. 

Regards, 
Manisankar Durai. 



AA Andrias Abadi replied to Mani Sankar Durai March 30, 2018 02:17 PM UTC

Hi Richard, 

We have checked your code example and we are able to reproduce the reported issue. Since you are performing server side filtering operation after the aggregation. So while handling aggregation before filtering it will retrieve the result based on all data. To avoid the issue we suggest you to handling aggregation based on filtered record instead of whole data (set the aggregation code after performing filtering). 
Please refer the code example 
  public ActionResult UrlDataSource(DataManager dm) 
        { 
 
            IEnumerable Data = new NorthwindDataContext().OrdersViews.ToList(); 
            DataResult result = new DataResult(); 
            Syncfusion.JavaScript.DataSources.DataOperations operation = new Syncfusion.JavaScript.DataSources.DataOperations(); 
            List<string> aggregateFields = new List<string>(); 
            if (dm.Search != null) 
                Data = operation.PerformSearching(Data, dm.Search); 
            if (dm.Where != null) 
                Data = operation.PerformWhereFilter(Data, dm.Where, dm.Where[0].Condition);       //perform filtering  
 
            if (dm.Aggregates != null) 
            { 
                for (var i = 0; i < dm.Aggregates.Count; i++) 
                    aggregateFields.Add(dm.Aggregates[i].Field); 
                result.aggregate = operation.PerformSelect(Data, aggregateFields); 
            }           //perform aggregation. 
            result.count = Data.AsQueryable().Count(); 
          
            if (dm.Skip != null && dm.Skip != 0)//skiped while rendering checkbox 
                Data = operation.PerformSkip(Data, dm.Skip); 
            if (dm.Take != null && dm.Take != 0)//skiped while rendering checkbox 
                Data = operation.PerformTake(Data, dm.Take); 
            result.result = Data;//passed the whole dataSource directly for checkbox rendering 
            return Json(result, JsonRequestBehavior.AllowGet); 
        } 
 

Refer the online sample link 

Please let us know if you need further assistance. 

Regards, 
Manisankar Durai. 


Hi sir, 

I used the code for server side aggregation just like the one you provided

 if (dm.Where != null) 
                Data = operation.PerformWhereFilter(Data, dm.Where, dm.Where[0].Condition);       //perform filtering  
 
            if (dm.Aggregates != null) 
            { 
                for (var i = 0; i < dm.Aggregates.Count; i++) 
                    aggregateFields.Add(dm.Aggregates[i].Field); 
                result.aggregate = operation.PerformSelect(Data, aggregateFields); 
            }           //perform aggregation. 

but this thing returned very large amount of data back to the client, it selected the aggregate field on each entries...

this is the result passed back from the controller to the browser
  1. {,…}
    1. aggregate:[{TotalAmount: 3159800}, {TotalAmount: 916300}, {TotalAmount: 2054400}, {TotalAmount: 1850000},…]
      1. [0 … 99]
    2. count:6424
    3. groupDs:null
    4. result:[{ID: 8785, OrderNumber: "GBM/SO/032918/0054", SalesUsername: "",…},…]

this obviously caused the query to slow down.. it went up to 1.5~2.5s with 6400 records which is really not practical
is there anyway to simply do the SUM in the server side instead of selecting the value like this?

something like 
result.aggregate = operation.PerformSum(Data, aggregateFields); 


Thanks 


FS Farveen Sulthana Thameeztheen Basha Syncfusion Team April 2, 2018 04:50 PM UTC

Hi  Andrias, 

If you want to get the Totalvalue of the summary column, you need to return the value from the serverside as like the above code example and link:- 


Please get back to us if you need any further assistance. 

Regards, 

Farveen sulthana T 



Loader.
Up arrow icon