Current filter:
                                You should refresh the page.
                                Support Center

                                How to create a report based on hierarchical data

                                0

                                Generally, there is no standard method to use hierarchical data, i. e. the data that is organized into a tree-like structure (see the Hierarchical model) as a datasource for the XtraReport. However, you can manually implement an effective and generic algorithm that performs data transformation from the hierarchical structure to the relational one.

                                This example demonstrates how it can be done. The core of this example is the RepTreeListHierarchicalDataRelationships.DataSetHelper.HierarchicalListToDataSet<T>() generic method, which transforms a List<T> to the DataSet. Also, please take special note of the fact that the RepTreeListHierarchicalDataRelationships.Form1.GenerateReport() method, which dynamically generates a report, based on the resulting DataSet.

                                You must  log in  or  register  to leave comments
                                Select file
                                • Form1.cs
                                • Program.cs
                                • HierarchicalData.cs
                                • HierarchyToDataSet.cs
                                • XtraReport1.cs
                                Select language
                                • C#
                                • VB.NET
                                Select version
                                • v2012 vol 2.11 - v2013 vol 2.9
                                • v2009 vol 1.5 - v2012 vol 1.12
                                using System;
                                using System.Collections.Generic;
                                using System.Data;
                                using System.Drawing;
                                using System.Windows.Forms;
                                using DevExpress.XtraReports.UI;
                                
                                namespace RepTreeListHierarchicalDataRelationships {
                                    public partial class Form1 : Form {
                                        private XtraReport report;
                                        private DataSet dataSet;
                                        private int indent = 0;
                                
                                        public Form1() {
                                            InitializeComponent();
                                        }
                                
                                        private void button1_Click(object sender, EventArgs e) {
                                            List<Employee> employes = Employee.GenerateSeveralEmployes();
                                
                                            dataSet = DataSetHelper.HierarchicalListToDataSet(employes, "Id", "ParentId");
                                
                                            DataSetHelper2.FillTreeView(treeView1, dataSet);
                                            treeView1.CollapseAll();
                                            button2.Enabled = true;
                                        }
                                
                                        private void button2_Click(object sender, EventArgs e) {
                                            GenerateReport();
                                            new ReportPrintTool(report).ShowPreviewDialog();
                                        }
                                
                                        private void GenerateReport() {
                                            ReportHeaderBand headerBand = new ReportHeaderBand();
                                            DetailBand detailBand = new DetailBand();
                                            XRLabel title = new XRLabel();
                                            string dataPath = dataSet.Tables[0].TableName;
                                
                                            report = new XtraReport();
                                            report.DataMember = dataPath;
                                
                                            headerBand.Height = 50;
                                            detailBand.Height = 25;
                                            title.Size = new Size(report.PageWidth - report.Margins.Left - report.Margins.Right - 1, 50);
                                            title.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                                            title.Font = new Font(title.Font.FontFamily, 20);
                                            title.Text = "Hierarchical Data Report";
                                
                                            headerBand.Controls.Add(title);
                                            report.Bands.Add(headerBand);
                                            report.Bands.Add(detailBand);
                                
                                            AddDataFieldsToBand(dataSet.Tables[0], detailBand, dataPath, false);
                                
                                            XtraReportBase nextReport = report;
                                
                                            for(int i = 0; i < dataSet.Relations.Count; i++) {
                                                DataRelation dataRelation = dataSet.Relations[i];
                                
                                                dataPath += "." + dataRelation.RelationName;
                                                nextReport = CreateReportForTable(nextReport, dataRelation.ChildTable, dataPath);
                                            }
                                
                                            report.DataSource = dataSet;
                                        }
                                
                                        private void AddDataFieldsToBand(DataTable dataTable, DetailBand band, string dataPath, bool addKeyFields) {
                                            XRTable table = XRTable.CreateTable(new Rectangle(indent, 0, report.PageWidth - report.Margins.Left - report.Margins.Right - indent - 1, 25), 1, dataTable.Columns.Count - (addKeyFields ? 0 : 2));
                                            int cellIndex = 0;
                                
                                            for(int i = 0; i < dataTable.Columns.Count; i++) {
                                                DataColumn dataColumn = dataTable.Columns[i];
                                
                                                if(addKeyFields || (!addKeyFields && !dataColumn.ColumnName.ToUpper().Contains("ID")))
                                                    table.Rows[0].Cells[cellIndex++].DataBindings.Add("Text", null, dataPath + "." + dataColumn.ColumnName);
                                            }
                                
                                            table.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0);
                                            band.Controls.Add(table);
                                            indent += 50;
                                        }
                                
                                        private XtraReportBase CreateReportForTable(XtraReportBase parentReport, DataTable dataTable, string dataPath) {
                                            DetailReportBand detailReport = new DetailReportBand();
                                            DetailBand detailBand = new DetailBand();
                                
                                            detailBand.Height = 25;
                                            detailReport.Bands.Add(detailBand);
                                            parentReport.Bands.Add(detailReport);
                                            AddDataFieldsToBand(dataTable, detailBand, dataPath, false);
                                            detailReport.DataMember = dataPath;
                                
                                            return detailReport;
                                        }
                                
                                    }
                                
                                }

                                To start a chat you should create a support ticket


                                If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

                                FOLLOW US

                                DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

                                Copyright © 1998-2014 Developer Express Inc.
                                All trademarks or registered trademarks are property of their respective owners