Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

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)
                        {
                            self.PrintingSystem.InsertPageBreak(0);
                        }
 
                        grid.FilterExpression = new GroupOperator(GroupOperatorType.And,
                            CriteriaOperator.Parse(filterExpression), new BinaryOperator(groupField, groupValues[i])).ToString();
                        grid.ExpandAll();
                        grid.DataBind();


                        BrickModifier skipArea = linkdata.SkipArea;
                        linkdata.SkipArea = self.SkipArea;
                        linkdata.AddSubreport(System.Drawing.PointF.Empty);
                        linkdata.SkipArea = skipArea;

                        grid.FilterExpression = filterExpression;
                    } 
                });

                clink.CreateDocument();

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

                Response.Clear();
                Response.Buffer = true;
                Response.AppendHeader("Content-Type","application/pdf");
                Response.AppendHeader("Content-Transfer-Encoding", "binary");
                Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}.pdf", reportFileName));
                Response.BinaryWrite(stream.ToArray());
                Response.End();               
            }; 

        }

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)
        {
            grid.DataBind();
            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

Top 10 Visual Studio things which can boost developers coding speed

Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.


Visual Studio 2008 Shell and TFS integration

Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.


Assembla - Free and private repository to manage your source code online with SVN subversion hosting

With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.


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.


How to select all checkboxes as per range of numbers in jQuery MVC

JQuery code to select all checkboxes as per range provided with select all and deselect all functionality which can be embedded in ASP.NET MVC too. With validations such as invalid range.


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!
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!

x