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
130
Hierarchical Grid Help
posted

I'm having some trouble with the Hierarchical Grid. I get a NullReferenceException in the view at run time. I followed this example https://www.igniteui.com/hierarchical-grid/load-on-demand and below is my code...

Controller

        [HttpGet]
        public ActionResult Index()
        {
            var model = _modelBuilder.BuildExportFileModel(this.CurrentUser.CompanyID, this.CurrentUser.UserID);

            model.ExportFiles = GetGridModel(true);

            return View("ExportFiles", model);
        }

        [ActionName("aspnet-mvc-helper")]
        public ActionResult AspMvcHelper()
        {
            GridModel gridModel = GetGridModel(true);
            return View("aspnet-mvc-helper", gridModel);
        }

        [GridDataSourceAction]
        [ActionName("dataset-binding")]
        public ActionResult DataSetMvcHelper()
        {
            var exportFilesRecordTypes = this.ExportFilesRecordTypes;
            return View("dataset-binding", exportFilesRecordTypes);
        }

        [ActionName("load-on-demand")]
        public ActionResult DataSetWithLoadOnDemand()
        {
            GridModel grid = GridLoadOnDemandModel();
            grid.ID = "gridLoadOnDemand";
            grid.LoadOnDemand = true;
            grid.DataSourceUrl = this.Url.Action("BindParent");
            grid.ColumnLayouts[0].DataSourceUrl = this.Url.Action("BindChild");
            return View("load-on-demand", grid);
        }

        private GridModel GetGridModel(bool enablePaging)
        {
            GridModel gridModel = new GridModel();

            //options
            gridModel.ID = "gridModel";
            gridModel.PrimaryKey = "ExportFileID";
            gridModel.AutoGenerateLayouts = false;
            gridModel.AutoGenerateColumns = false;
            gridModel.Width = "100%";

            // set default column options
            gridModel.Columns.Add(new GridColumn() { Key = "ExportFileID", HeaderText = "ExportFileID", DataType = "number", Width = "100%" });
            gridModel.Columns.Add(new GridColumn() { Key = "Description", HeaderText = "Description", DataType = "string", Width = "100%" });
            gridModel.Columns.Add(new GridColumn() { Key = "CompanyID", HeaderText = "CompanyID", DataType = "number", Width = "100%" });
            gridModel.Columns.Add(new GridColumn() { Key = "ExportFileTypeID", HeaderText = "ExportFileTypeID", DataType = "number", Width = "100%" });
            gridModel.Columns.Add(new GridColumn() { Key = "ReportExportTypeID", HeaderText = "ReportExportTypeID", DataType = "number", Width = "100%" });

            gridModel.DataSourceUrl = this.Url.Action("GetExportFilesData");
            gridModel.DataSource = _masterRefService.GetExportFiles(this.CurrentUser.CompanyID, this.CurrentUser.UserID).AsQueryable();

            GridColumnLayoutModel recordTypesModel = GetRecordTypesLayout();
            recordTypesModel.AutoFormat = GridAutoFormat.DateAndNumber;
            gridModel.ColumnLayouts.Add(recordTypesModel);

            //paging 
            GridPaging paging = new GridPaging() { PageSize = 20, Type = OpType.Remote };

            //hiding
            GridHiding hiding = new GridHiding() { ColumnChooserContainment = "window", ColumnChooserHeight = "450px" };

            //sorting
            GridSorting sorting = new GridSorting() { Type = OpType.Remote, Mode = SortingMode.Single };

            //resizing
            GridResizing resizing = new GridResizing() { AllowDoubleClickToResize = true };            
            
            gridModel.Features.Add(sorting);
            gridModel.Features.Add(hiding);
            gridModel.Features.Add(resizing);

            //if the "select all" is requesting data, disable paging to include all records; select all call comes from GetFilteredIDs();
            if (enablePaging)
            {
                gridModel.Features.Add(paging);
            }

            return gridModel;
        }

        private GridModel GridLoadOnDemandModel()
        {
            // Define the Categories layout
            GridModel grid = new GridModel();
            grid.AutoGenerateLayouts = false;
            grid.AutoGenerateColumns = false;
            grid.PrimaryKey = "ExportFileID";
            grid.Width = "100%";
            grid.Columns.Add(new GridColumn() { HeaderText = "ExportFileRecordTypeID", Key = "ExportFileRecordTypeID", DataType = "number", Width = "100%" });
            grid.Columns.Add(new GridColumn() { HeaderText = "ExportFileID", Key = "ExportFileID", DataType = "number", Width = "100%" });
            grid.Columns.Add(new GridColumn() { HeaderText = "Description", Key = "Description", DataType = "number", Width = "100%" });
            grid.Columns.Add(new GridColumn() { HeaderText = "Sequence", Key = "Sequence", DataType = "number", Width = "100%" });

            // Define the Products layout
            GridColumnLayoutModel layout = new GridColumnLayoutModel();
            layout.Key = "RecordTypes";
            layout.PrimaryKey = "ExportFileRecordTypeID";
            layout.ForeignKey = "ExportFileID";
            layout.Width = "100%";
            layout.AutoGenerateColumns = false;
            //layout.AutofitLastColumn = false;
            layout.Columns = new List<GridColumn>();
            layout.Columns.Add(new GridColumn() { HeaderText = "ExportFileID", Key = "ExportFileID", DataType = "number", Width = "100%" });
            layout.Columns.Add(new GridColumn() { HeaderText = "ExportFileRecordTypeID", Key = "ExportFileRecordTypeID", DataType = "number", Width = "100%" });
            layout.Columns.Add(new GridColumn() { HeaderText = "Description", Key = "Description", DataType = "string", Width = "100%" });
            layout.Columns.Add(new GridColumn() { HeaderText = "Sequence", Key = "Sequence", DataType = "number", Width = "100%" });

            layout.Width = "100%";
            grid.ColumnLayouts.Add(layout);

            GridPaging paging = new GridPaging();
            paging.Type = OpType.Remote;
            paging.Inherit = true;
            paging.PageSize = 5;
            grid.Features.Add(paging);

            return grid;
        }

        private GridColumnLayoutModel GetRecordTypesLayout()
        {
            GridColumnLayoutModel layout = new GridColumnLayoutModel();
            layout.Key = "RecordTypes";
            layout.PrimaryKey = "ExportFileRecordTypeID";
            layout.ForeignKey = "ExportFileID";
            layout.Width = "100%";
            layout.AutoGenerateColumns = false;
            //layout.AutofitLastColumn = false;
            layout.Columns = new List<GridColumn>();
            layout.Columns.Add(new GridColumn() { HeaderText = "ExportFileRecordTypeID", Key = "ExportFileRecordTypeID", DataType = "number", Width = "100%" });
            layout.Columns.Add(new GridColumn() { HeaderText = "ExportFileID", Key = "ExportFileID", DataType = "number", Width = "100%" });
            layout.Columns.Add(new GridColumn() { HeaderText = "Description", Key = "Description", DataType = "number", Width = "100%" });
            layout.Columns.Add(new GridColumn() { HeaderText = "Sequence", Key = "Sequence", DataType = "number", Width = "100%" });
            layout.DataSource = _masterRefService.GetExportFileRecordTypesData(this.CurrentUser.CompanyID, this.CurrentUser.UserID).AsQueryable();
            layout.DataSourceUrl = this.Url.Action("GetRecordTypesData");
            return layout;
        }

        public JsonResult GetExportFilesData()
        {
            GridModel model = GetGridModel(true);
            return model.GetData();
        }

        public JsonResult GetRecordTypesData(string path, string layout)
        {
            GridModel gridModel = GetGridModel(true);
            gridModel.DataSource = _masterRefService.GetExportFileRecordTypesData(this.CurrentUser.CompanyID, this.CurrentUser.UserID).AsQueryable();
            return gridModel.GetData(path, layout);
        }

        public JsonResult BindParent()
        {
            GridModel model = GridLoadOnDemandModel();
            model.DataSource = _masterRefService.GetExportFiles(this.CurrentUser.CompanyID, this.CurrentUser.UserID).AsQueryable();
            return model.GetData();
        }

        public JsonResult BindChild(string path, string layout)
        {
            GridModel model = GridLoadOnDemandModel();
            model.DataSource = _masterRefService.GetExportFileRecordTypesData(this.CurrentUser.CompanyID, this.CurrentUser.UserID).AsQueryable();
            return model.GetData(path, layout);
        }

        private DataSet ExportFilesRecordTypes
        {
            get
            {
                if (Session["ExportFilesRecordTypes"] == null)
                {
                    Session["ExportFilesRecordTypes"] = GetExportFilesRecordTypesDataSet();
                }
                return (DataSet)Session["ExportFilesRecordTypes"];
            }
        }

        private DataSet GetExportFilesRecordTypesDataSet()
        {
            DataSet ds = new DataSet("ExportFilesRecordTypes");

            // Get the tables to use in the DataSet
            DataTable exportFiles = new DataTable();
            exportFiles.Columns.Add(new DataColumn("ExportFileID"));
            exportFiles.Columns.Add(new DataColumn("CompanyID"));
            exportFiles.Columns.Add(new DataColumn("ExportFileTypeID"));
            exportFiles.Columns.Add(new DataColumn("ReportExportTypeID"));
            exportFiles.Columns.Add(new DataColumn("Description"));
            foreach (var exportFile in _masterRefService.GetExportFiles(this.CurrentUser.CompanyID, this.CurrentUser.UserID))
            {
                var row = exportFiles.NewRow();
                row["ExportFileID"] = exportFile.ExportFileID;
                row["CompanyID"] = exportFile.CompanyID;
                row["ExportFileTypeID"] = exportFile.ExportFileTypeID;
                row["ReportExportTypeID"] = exportFile.ReportExportTypeID;
                row["Description"] = exportFile.Description;
            }

            DataTable recordTypes = new DataTable();
            recordTypes.Columns.Add(new DataColumn("ExportFileRecordTypeID"));
            recordTypes.Columns.Add(new DataColumn("ExportFileID"));
            recordTypes.Columns.Add(new DataColumn("Description"));
            recordTypes.Columns.Add(new DataColumn("Sequence"));
            foreach (var recordType in _masterRefService.GetExportFileRecordTypesData(this.CurrentUser.CompanyID, this.CurrentUser.UserID))
            {
                var row = recordTypes.NewRow();
                row["ExportFileRecordTypeID"] = recordType.ExportFileRecordTypeID;
                row["ExportFileID"] = recordType.ExportFileID;
                row["Description"] = recordType.Description;
                row["Sequence"] = recordType.Sequence;
            }

            // Add the tables to the DataSet
            ds.Tables.Add(exportFiles);
            ds.Tables.Add(recordTypes);

            // Create the relationship between the tables.
            ds.Relations.Add("ExportFileRecordTypeFileDefinitionRel1", exportFiles.Columns["ExportFileID"], recordTypes.Columns["ExportFileID"]);
            return ds;
        }
 

View

@using Infragistics.Web.Mvc
@model Optimum.WebUI.Areas.CodeTables.Models.ExportFile.ExportFileModel

<div class="row">
    <div class="col-xs-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title"><i class="fa fa-table fa-lg"></i>&nbsp;Export Files</h4>
            </div>
            @(Html.Infragistics().Grid(Model.ExportFiles))
        </div>
    </div>
</div>
}

  • 130
    Verified Answer
    Offline posted

    I've got this working and wanted to post the answer for anyone else having trouble. I stripped this down as basic as I could and opted to not use LoadOnDemand. The main issue was with my Models as they were not formed correctly and only one DataSource was needed for the parent GridModel. 

    Controller

     [HttpGet]
            public ActionResult Index()
            {
                var model = _modelBuilder.BuildExportFileModel(this.CurrentUser.CompanyID, this.CurrentUser.UserID);
    
                model.ExportFiles = GetGridModel();
    
                return View("ExportFiles", model);
            }
    
            #region igGrid
    
            private GridModel GetGridModel()
            {
                //define parent layout
                GridModel grid = new GridModel();
                grid.ID = "ExportFiles";
                grid.AutoGenerateLayouts = false;
                grid.AutoGenerateColumns = false;
                grid.PrimaryKey = "ExportFileID";
                grid.Width = "100%";
                grid.Columns.Add(new GridColumn() { Key = "CompanyID", HeaderText = "CompanyID", DataType = "string", Width = "100%", Hidden = true });
                grid.Columns.Add(new GridColumn() { Key = "Description", HeaderText = "Description", DataType = "string", Width = "100%" });       
                grid.Columns.Add(new GridColumn() { Key = "ExportFileID", HeaderText = "ExportFileID", DataType = "number", Width = "100%" });
                grid.Columns.Add(new GridColumn() { Key = "ExportFileTypeID", HeaderText = "ExportFileTypeID", DataType = "number", Width = "100%" });
                grid.Columns.Add(new GridColumn() { Key = "ReportExportTypeID", HeaderText = "ReportExportTypeID", DataType = "number", Width = "100%" });     
    
                //define child layout
                GridColumnLayoutModel layout = new GridColumnLayoutModel();
                layout.Key = "RecordTypes";
                layout.ForeignKey = "ExportFileID";
                layout.PrimaryKey = "ExportFileRecordTypeID";
                layout.AutoGenerateColumns = false;
                layout.Columns = new List<GridColumn>();
                layout.Columns.Add(new GridColumn() { HeaderText = "Description", Key = "Description", DataType = "string", Width = "100%" });
                layout.Columns.Add(new GridColumn() { HeaderText = "ExportFileRecordTypeID", Key = "ExportFileRecordTypeID", DataType = "number", Width = "100%" });
                layout.Columns.Add(new GridColumn() { HeaderText = "ExportFileID", Key = "ExportFileID", DataType = "number", Width = "100%" });
                layout.Columns.Add(new GridColumn() { HeaderText = "Sequence", Key = "Sequence", DataType = "number", Width = "100%" });
    
                layout.Width = "100%";
                grid.ColumnLayouts.Add(layout);
    
                grid.DataSource = GetExportFiles().AsQueryable();
    
                //GridPaging paging = new GridPaging();
                //paging.Type = OpType.Remote;
                //paging.Inherit = true;
                //paging.PageSize = 5;
                //grid.Features.Add(paging);
    
                return grid;
            }
    
            private List<ExportFileItem> GetExportFiles()
            {
                var exportFileItems = new List<ExportFileItem>();
                foreach (var exportFile in _masterRefService.GetExportFiles(this.CurrentUser.CompanyID, this.CurrentUser.UserID))
                {
                    var item = new ExportFileItem()
                    {
                        ExportFileID = exportFile.ExportFileID,
                        CompanyID = exportFile.CompanyID,
                        ExportFileTypeID = exportFile.ExportFileTypeID,
                        ReportExportTypeID = exportFile.ReportExportTypeID,
                        Description = exportFile.Description,
                        RecordTypes = _masterRefService.GetExportFileRecordTypes(this.CurrentUser.CompanyID, this.CurrentUser.UserID, exportFile.ExportFileID).ToList()
                    };
                    exportFileItems.Add(item);
                }
    
                return exportFileItems;
            }
                   
            private class ExportFileItem
            {
                public ExportFileItem()
                {
                    RecordTypes = new List<ExportFileRecordType>();
                }
                public int? ExportFileID { get; set; }
                public string CompanyID { get; set; }
                public int? ExportFileTypeID { get; set; }
                public int? ReportExportTypeID { get; set; }
                public string Description { get; set; }
    
                public IEnumerable<ExportFileRecordType> RecordTypes { get; set; }
            }
    
            #endregion