Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
65
WDG and WDE, Applying Filters Dynamically in Code and Exporting?
posted

So I'm essentially sending filter, sorting, and other data from a page that has an iggrid. The idea is to take this info and then apply it to a WebDataGrid control. 

For some reason, I can't get the filters I'm adding to the grid to apply when exporting with web document exporter. The style, the column selection all appears to show up, but the filters just won't apply. Either I get data with no filters or no data. Is there a good example or sample of how I can apply filters to a grid programatically??

Here's what I'm trying to do. I included the void that I'm using in an mvc project to download the PDF. I've also included the three options classes.

public void GetGridAsPDF(string reportName, string parameters, string columnsShown, string filterData, string sortData)
        {
            var serializer = new JavaScriptSerializer();
            var hidingOptions = serializer.Deserialize<List<HidingOption>>(columnsShown);
            var filterOptions = serializer.Deserialize<List<FilterOption>>(filterData);
            var sortOptions = serializer.Deserialize<List<SortOption>>(sortData);

            var grid = new WebDataGrid();
            grid.CssClass = "reportGrid";
            grid.HeaderCaptionCssClass = "reportHeader";
            grid.ItemCssClass = "reportCell";
            grid.AltItemCssClass = "reportCellAlt";
            grid.FooterCaptionCssClass = "reportFooter";
            var exporter = new WebDocumentExporter();
            var page = new System.Web.UI.Page();
            page.Controls.Add(grid);
            page.Controls.Add(exporter);

            switch (reportName)
            {
                case "reportNameHere":
                    var context = new Entities();
                    var data = context.sp_report_reportNameSP(int.Parse(parameters.CSVToArray()[0])).ToList();
                    grid.Columns.Clear();
                  

                    data.Select(p => p).First().GetType().GetProperties().ToList().ForEach((p) =>
                    {
                        var dataField = new BoundDataField();
                        dataField.DataFieldName = p.Name;
                        dataField.Key = p.Name;
                        dataField.EnableMultiline = true;
                        dataField.HtmlEncode = true;
                        dataField.Header.Text = ((hidingOptions.Any(h => h.key == p.Name)) ? hidingOptions.First(h => h.key == p.Name).headerText : p.Name);
                        dataField.Hidden = ((hidingOptions.Any(h => h.key == p.Name)) ? hidingOptions.First(h => h.key == p.Name).hidden : true);                        
                        grid.Columns.Add(dataField);
                    });


                 
                    grid.Behaviors.CreateBehavior<Sorting>();
                    grid.Behaviors.CreateBehavior<Filtering>();

                    grid.Behaviors.Filtering.Enabled = true;
                    grid.Behaviors.Sorting.Enabled = true;


                    filterOptions.ForEach((f) =>
                    {
                        var filter = new ColumnFilter();
                        filter.ColumnKey = f.fieldName;

                        switch (f.type)
                        {
                            case "string":
                                filter.Condition = new RuleTextNode((TextFilterRules)Enum.Parse(typeof(TextFilterRules), f.cond, true), f.expr);
                                break;
                            case "number":
                                filter.Condition = new RuleNumberNode((NumericFilterRules)Enum.Parse(typeof(NumericFilterRules), f.cond, true), (f.expr != "") ? double.Parse(f.expr) : 0);
                                break;
                            case "date":
                                filter.Condition = new RuleDateNode((DateTimeFilterRules)Enum.Parse(typeof(DateTimeFilterRules), f.cond, true), (f.expr != "") ? DateTime.Parse(f.expr) : DateTime.Now);
                                break;
                            case "bool":
                                filter.Condition = new RuleBoolNode((BooleanFilterRules)Enum.Parse(typeof(BooleanFilterRules), f.cond, true));
                                break;

                        }
                        grid.Behaviors.Filtering.ColumnFilters.Add(filter);
                    });


                    grid.Behaviors.Sorting.SortingMode = Infragistics.Web.UI.GridControls.SortingMode.Multi;
                    sortOptions.Where(s => s.isSorting).ToList().ForEach((s) =>
                    {
                        grid.Behaviors.Sorting.SortedColumns.Add(grid.Columns[s.fieldName], (s.dir == "asc") ? SortDirection.Ascending : (s.dir == "desc") ? SortDirection.Descending : SortDirection.Ascending);
                    });

                    grid.Visible = true;
                    exporter.Visible = true;

                    grid.EnableTheming = true;
                    grid.StyleSetName = "Default";
                    var report = new Infragistics.Documents.Reports.Report.Report();
                    var section = report.AddSection();
                    var header = section.AddText();
                    header.AddContent("Report Title Here");
                    header.Style.Font.Size = 16f;
                    header.Style.Font.Bold = true;
                    header.Alignment = new Infragistics.Documents.Reports.Report.TextAlignment(Infragistics.Documents.Reports.Report.Alignment.Center);
                    var gap = section.AddGap();
                    gap.Height = new Infragistics.Documents.Reports.Report.FixedHeight(20);


                    grid.DataSource = data;
                    grid.AutoGenerateColumns = false;
                    grid.DataBind();

                    grid.Behaviors.Filtering.ApplyFilter();
                 
                                   
                    exporter.ExportMode = ExportMode.Download;
                    exporter.DownloadName = reportName + ".pdf";
                    exporter.Format = FileFormat.PDF;
                    exporter.CellExporting += exporter_CellExporting;
                    exporter.EnableStylesExport = true;

                    exporter.DataExportMode = DataExportMode.DataInGridOnly;



                    exporter.TargetPaperOrientation = Infragistics.Documents.Reports.Report.PageOrientation.Landscape;
                    exporter.TargetPaperSize = Infragistics.Documents.Reports.Report.PageSizes.Legal;

                    exporter.Export(grid, report, section);

                    break;



            }



        }
		
		
 public class FilterOption
    {
        public string fieldName;
        public string cond;
        public string expr;
        public string logic;
        public string type;
    }

    public class HidingOption
    {
        public string headerText;
        public string key;
        public string dataType;
        public bool hidden;
        public int? width;
    }

    public class SortOption
    {
        public string fieldName;
        public bool isSorting;
        public string dir;
    }