Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


How to add Page break after each group in DevExpress Grid Export

Share with WhatsApp

This post covers information about how to add page break after each group while exporting DevExpress Grid in ASP.NET MVC 4.

In one of my project there was requirement in which I need to add a page break after each group in DevExpress MVC Grid Export and I was just searching and trying the different code but not getting the desired output. After lots of attempts I posted a question on DevExpress support and received a reply that this functionality is not supported for MVC and only available for ASPX grid and winform grid. But I kept trying and finally achieved it in MVC so thought better to share it as it will definetly save a time if you too having a requirement like this.

Code is almost similar to the code shared in this link,but this code is for ASPxGridView so needed some alteration to get it work in MVC.

Suppose there is a controller method like below which takes export setting model as input which contains information such as report name, title, currently applied filter on grid etc.

  public ActionResult ExportToPDF(ExportSettingsViewModel exportModel)
            List<ModelOfDataToShow> model = GetData();//load data from your business layer
            return GridViewExtension.ExportToPdf(GetGridViewSettings(exportModel.ReportFileName, exportModel.ReportTitle, exportModel.CurrentlyAppliedFilter), model);                   

In above method we have returned ActionResult by using ExportToPdf method of GridViewExtension class which holds parameters such as GridViewSettings and data i.e. model.

So we have first loaded our data in object named model and for GridViewSetting we have another method named "GetGridViewSettings" like below.

public GridViewSettings GetGridViewSettings(string reportFileName, string reportTitle, string filterExpression)
            var settings = new GridViewSettings();
            settings.Name = "GridView";

            //Do some export specific settings margins,paperkind etc 
            settings.SettingsExport.FileName = reportFileName;

            //add columns
            settings.Columns.Add("EmpId", "Employee Id");
            settings.Columns.Add("EmpName", "Name");
            settings.Columns.Add("DeptId", "Department Id").GroupIndex = 1;

            settings.SettingsExport.BeforeExport = (sender, e) =>
                MVCxGridView grid = (MVCxGridView)sender;
                grid.FilterExpression = filterExpression; 

                string groupField = grid.GetGroupedColumns()[0].FieldName;
                object[] groupValues = FindGroupValues(grid);
                PrintingSystem ps = new PrintingSystem();
                Link clink = new Link(ps);

                BrickGraphics gr = ps.Graph;

                clink.CreateDetailArea += new CreateAreaEventHandler(delegate(object sender2, CreateAreaEventArgs e2)
                    Link self = (Link)sender2;  
                    for (int i = 0; i < groupValues.Length; i++)
                        MVCxGridViewExporter exporter = new MVCxGridViewExporter(grid);
                        DevExpress.Web.ASPxGridView.Export.Helper.GridViewLink linkdata = new DevExpress.Web.ASPxGridView.Export.Helper.GridViewLink(exporter);
                        linkdata.PrintingSystemBase = self.PrintingSystem;

                        if (i > 0)
                        grid.FilterExpression = new GroupOperator(GroupOperatorType.And,
                            CriteriaOperator.Parse(filterExpression), new BinaryOperator(groupField, groupValues[i])).ToString();

                        BrickModifier skipArea = linkdata.SkipArea;
                        linkdata.SkipArea = self.SkipArea;
                        linkdata.SkipArea = skipArea;

                        grid.FilterExpression = filterExpression;


                System.IO.MemoryStream stream = new System.IO.MemoryStream(); 

                Response.Buffer = true;
                Response.AppendHeader("Content-Transfer-Encoding", "binary");
                Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}.pdf", reportFileName));


In above method if you notice we have handled "settings.SettingsExport.BeforeExport" after adding columns and completing some basic export specific settings. The code in BeforeExport is the actual code which adds page break after each group.

There is one more function is used to get all group key values named "FindGroupValues" which are as follows.

 public static object[] FindGroupValues(ASPxGridView grid)
            string column = grid.GetGroupedColumns()[0].FieldName;
            System.Collections.ArrayList list = new System.Collections.ArrayList();
            for (int i = 0; i < grid.VisibleRowCount; i++)
                if (grid.GetRowLevel(i) == 0)
                    list.Add(grid.GetRowValues(i, column));
            return list.ToArray();

PrintingSystem and Link are the most important part of the above code which actually adds page break.

Hope you have benefited from the above code. Let me know in comment section if you are facing any problems while achieving the above functionality. Thanks.

If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.

Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.

How to post JSON string as a string in jQuery AJAX

Know how to post JSON String as string to the action method of controller in MVC.

How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.

Your opinion is valuable for us! Comments, suggetions are welcome.

Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!