I am struggling to hide some columns in the child rows of a hierarchical grid bound to a dataset with two tables, with a parent:child relationship.
I am looking at the hierarchical grid data set binding sample that came with NetAdvantage and I am trying to hide the CategoryID column in the child rows since it is merely repeating the same value in the parent row.
Here is the grid definition from the MVC View (dataset-binding.cshtml), with my additions in bold.:
@(Html.Infragistics().Grid<Category>() .ID("grid1") .Height("500px") .AutoGenerateColumns(true) .AutoGenerateLayouts(true) .RenderCheckboxes(true) .EnableUTCDates(true) .Features(features => { features.Sorting().Type(OpType.Local).Mode(SortingMode.Single).Inherit(true); features.Paging().PageSize(5).Type(OpType.Remote).Inherit(false); features.Filtering().Type(OpType.Local).Inherit(true); features.Selection().Mode(SelectionMode.Row).MultipleSelection(true); features.RowSelectors().Inherit(true); features.GroupBy().Type(OpType.Local).Inherit(true); features.Hiding().Inherit(true); features.Resizing().Inherit(true); features.Tooltips().Visibility(TooltipsVisibility.Always).Inherit(true); features.ColumnMoving().Inherit(true).Mode(MovingMode.Immediate).MoveType(MovingType.Dom); features.CellMerging().InitialState(CellMergingInitialState.Merged); }) .ColumnLayouts(layouts => { layouts.For(x => x.Products) .PrimaryKey("CategoryID") .ForeignKey("ProductID").Columns(column => { column.For(x => x.CategoryID).DataType("numeric").Hidden(true); column.For(x => x.ID).DataType("numeric"); column.For(x => x.ProductName).DataType("string"); column.For(x => x.UnitPrice).DataType("numeric"); column.For(x => x.UnitsInStock).DataType("numeric"); column.For(x => x.Discontinued).DataType("bool"); }) ; }) .DataSource(Model) .DataSourceUrl(Url.Action("dataset-binding")) .DataBind() .Render() )
When I run the sample up I get a KeyNotFoundException with the message "The given key was not present in the dictionary".
Stack trace:
at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at Infragistics.Web.Mvc.GridModel.RenderHierarchicalQueryableRecursive(IQueryable queryable, WrappedGridResponse response, GridModel baseLayout) at Infragistics.Web.Mvc.GridModel.RenderHierarchicalQueryableRecursive(IQueryable queryable, WrappedGridResponse response, GridModel baseLayout) at Infragistics.Web.Mvc.GridModel.RenderHierarchicalQueryable(IQueryable queryable) at Infragistics.Web.Mvc.GridModel.DataBind() at Infragistics.Web.Mvc.Grid`1.DataBind() at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at ASP._Page_Views_hierarchicalgrid_dataset_binding_cshtml.Execute() in c:\Users\Public\Documents\Infragistics\IgniteUI 2013.1\Samples\IgniteUI.SamplesBrowser\Views\HierarchicalGrid\dataset-binding.cshtml:line 26 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.b__17() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
I have tried all sorts of variations but my gut feeling is that it boils down to this bit: layouts.For(x => x.Products).
I don't know what the right name is for the child collection. There is not much else I can choose for the value, but all the column names are correct so I suspect that this needs to be something else.
Regards,
Graeme
Forgot to mention that this is using NetAdvantage v13.1
Hello Graeme,
Please refer to the following link for this:
http://help.infragistics.com/Doc/jquery/2013.1/CLR4.0/?page=igGrid_Configure_Column_Hiding.html.
The code should be something like this, you can alter it if needed for your own needs:\
features.Hiding().ColumnSettings(settings => settings.ColumnSetting()
.ColumnKey("ID")
.AllowHiding(true)
.Hidden(true));
Please note that you need to specify the column by its key, make sure that it is the right one.
Please feel free to let me know if a question about our tool set comes up on your mind.
Hi
Thanks for the prompt response.
I'm afraid that I don't know how to set the features on the child band - could you be a little more explicit with reference to the example I gave?
For my real world use I also need to set the column header text, format etc for the child band and I don't see how to refer to the child band for this purpose?
eg If I add some code for hiding as you suggest it only affects the parent rows - how do I make it affect the child rows?
Here is what I am trying to achieve (but it fails for reasons mentioned in my original post), in my real world case. It feels like the right code to use but I get this key not found problem, with no clue as to which key in which collection. You will see that I have quite a few columns to rename and hide in my child band:
@(Html.Infragistics().Grid<MyApp.MyModel>() .ID("grid1") .Width("100%") .DefaultColumnWidth("150px") .AutoGenerateColumns(true) .AutoGenerateLayouts(true) .PrimaryKey("TicketID") .Columns(column => { column.For(x => x.TicketID).DataType("numeric").Hidden(true); column.For(x => x.TicketURL).DataType("string").Hidden(true); column.For(x => x.CustomerIDX).HeaderText("Customer").Width("150px"); column.For(x => x.TicketIDX).HeaderText("Ticket").Width("50px"); column.For(x => x.Title).HeaderText("Title").Width("400px").Template("<a href='${TicketURL}' target='_blank'>${Title}</a>"); column.For(x => x.IsPrePay).HeaderText("Pre-Paid").Width("10px").DataType("bool").Width("50px").Format("checkbox"); column.For(x => x.InvoiceableHours).HeaderText("Invoiceable Hours").Width("50px"); }).Features(features => { features.Sorting().Type(OpType.Local).Mode(SortingMode.Single).Inherit(true); features.Paging().PageSize(40).Type(OpType.Remote).Inherit(false); features.Filtering().Type(OpType.Local).Inherit(true); features.Selection().Mode(SelectionMode.Row).MultipleSelection(true); features.GroupBy().Type(OpType.Local).Inherit(true); features.Hiding().ColumnSettings(settings => settings.ColumnSetting() .ColumnKey("CustomerIDX") .AllowHiding(true) .Hidden(true)); features.Hiding().ColumnSettings(settings => settings.ColumnSetting() .ColumnKey("TicketID") .AllowHiding(true) .Hidden(true)); }) .ColumnLayouts(layouts => { layouts.For(x => x.ChildRows) .PrimaryKey("TimesheetID") .ForeignKey("TicketID").Columns(column => { column.For(x => x.TimesheetID).DataType("numeric").Hidden(true); column.For(x => x.TimesheetIDX).DataType("numeric").Hidden(true); column.For(x => x.TicketID).DataType("numeric").Hidden(true); column.For(x => x.UserNameX).HeaderText("Agent").DataType("string"); column.For(x => x.UserIDX).HeaderText("Agent").DataType("string"); column.For(x => x.StartDate).HeaderText("Date").Width("50px"); column.For(x => x.Duration).HeaderText("Duration (Mins)").Width("50px"); column.For(x => x.IsReadyToInvoice).HeaderText("Ready to invoice?").Width("10px").DataType("bool"); column.For(x => x.HasBeenInvoiced).HeaderText("Invoiced?").Width("10px").DataType("bool"); column.For(x => x.IsBillable).HeaderText("Billable?").Width("10px").DataType("bool"); }) ; }) .DataSource(Model) .DataSourceUrl(Url.Action(InvoicePrepController.ACTION_InvoicePrepData, new { StartDate = ViewBag.StartDate, EndDate = ViewBag.EndDate })) .DataBind() .Render() )
Your help on this matter is much appreciated.
Regards
Are you able to point me in the right direction on this one?
Hi Graeme,
Would you please attach a sample project here so I can modify it with the needed details and explanations and send it back to you?
Thank you in advance!
The challenge would be to hide the CategoryID from the products tables in the view in your online sample here:
http://www.igniteui.com/hierarchical-grid/dataset-binding
I have, instead, used the technique described here:
http://www.igniteui.com/hierarchical-grid/editing-dataset
and implemented my own GetGridModel method in my controller. This is now working great and I am moving on to the save function.
Thank-you for your assistance so far but I have now avoided the problem.
I think that I will define the grid model in the controller from now on as it is straightforward and leads to a simpler View (because the View does not need to define the columns, layout etc, so it boils down to a one liner:
@Html.Infragistics().Grid("MyGridID", Model.MyGridModel)
For completeness, here is my controller method which sets up the GridModel and hides unwanted columns:
private void InitialiseInvoiceDataGridOptions(GridModel gridModel) { gridModel.DataMember = "Ticket"; gridModel.PrimaryKey = "TicketID"; gridModel.Columns.Add(new GridColumn("Customer", InvoicePrepDataViewModel.COL_T_CustomerName, "string", "100px")); gridModel.Columns.Add(new GridColumn("TicketID", InvoicePrepDataViewModel.COL_T_TicketID, "number", "100px")); gridModel.Columns.Add(new GridColumn("Ticket", InvoicePrepDataViewModel.COL_T_TicketIDX, "string", "100px")); gridModel.Columns.Add(new GridColumn("URL", InvoicePrepDataViewModel.COL_T_TicketURL, "string", "100px")); GridColumn oCol = new GridColumn("Title", InvoicePrepDataViewModel.COL_T_Title, "string", "300px"); oCol.Template = "<a href='${TicketURL}' target='_blank'>${Title}</a>"; gridModel.Columns.Add(oCol); gridModel.Columns.Add(new GridColumn("Service type", InvoicePrepDataViewModel.COL_T_ServiceType, "string", "100px")); gridModel.Columns.Add(new GridColumn("Charge type", InvoicePrepDataViewModel.COL_T_ChargeType, "string", "100px")); gridModel.Columns.Add(new GridColumn("Invoiceable hours", InvoicePrepDataViewModel.COL_T_InvoiceableHours, "number", "100px")); gridModel.Columns.Add(new GridColumn("Hours this invoice", InvoicePrepDataViewModel.COL_T_HoursToInvoice, "number", "100px")); oCol = new GridColumn("Pre-paid?", InvoicePrepDataViewModel.COL_T_IsPrePay, "bool", "100px"); oCol.Format = "checkbox"; gridModel.Columns.Add(oCol); GridHiding oHiding = new GridHiding(); oHiding.ColumnSettings.Add(new ColumnHidingSetting() { ColumnKey = InvoicePrepDataViewModel.COL_T_TicketID, AllowHiding = false, Hidden = true }); oHiding.ColumnSettings.Add(new ColumnHidingSetting() { ColumnKey = InvoicePrepDataViewModel.COL_T_TicketURL, AllowHiding = false, Hidden = true }); gridModel.Features.Add(oHiding); GridColumnLayoutModel layout = new GridColumnLayoutModel(); layout.Key = "Timesheet"; layout.ForeignKey = "TicketID"; layout.PrimaryKey = "TimesheetID"; layout.Columns.Add(new GridColumn("TimesheetID", TimesheetViewModel.COL_TS_TimesheetID, "number", "100px")); layout.Columns.Add(new GridColumn("TimesheetIDX", TimesheetViewModel.COL_TS_TimesheetIDX, "number", "100px")); layout.Columns.Add(new GridColumn("TicketID", TimesheetViewModel.COL_TS_TicketID, "number", "100px")); layout.Columns.Add(new GridColumn("Duration", TimesheetViewModel.COL_TS_Duration, "number", "100px")); oCol = new GridColumn("Invoiced?", TimesheetViewModel.COL_TS_HasBeenInvoiced, "bool", "100px"); oCol.Format = "checkbox"; layout.Columns.Add(oCol); oCol = new GridColumn("Billable?", TimesheetViewModel.COL_TS_IsBillable, "bool", "100px"); oCol.Format = "checkbox"; layout.Columns.Add(oCol); oCol = new GridColumn("Ready to invoice?", TimesheetViewModel.COL_TS_IsReadyToInvoice, "bool", "100px"); oCol.Format = "checkbox"; layout.Columns.Add(oCol); layout.Columns.Add(new GridColumn("Agent", TimesheetViewModel.COL_TS_UserNameX, "string", "100px")); layout.Width = "100%"; layout.DataMember = "Timesheet"; oHiding = new GridHiding(); oHiding.ColumnSettings.Add(new ColumnHidingSetting() { ColumnKey = TimesheetViewModel.COL_TS_TicketID, AllowHiding = false, Hidden = true }); oHiding.ColumnSettings.Add(new ColumnHidingSetting() { ColumnKey = TimesheetViewModel.COL_TS_TimesheetID, AllowHiding = false, Hidden = true }); oHiding.ColumnSettings.Add(new ColumnHidingSetting() { ColumnKey = TimesheetViewModel.COL_TS_TimesheetIDX, AllowHiding = false, Hidden = true }); layout.Features.Add(oHiding); gridModel.ColumnLayouts.Add(layout); gridModel.Features.Add(new GridFiltering()); GridPaging paging = new GridPaging(); paging.Type = OpType.Remote; paging.Inherit = true; paging.PageSize = 25; gridModel.Features.Add(paging); }// InitialiseInvoiceDataGridOptions
I am glad to hear that you overcame this! Thank you for sharing this with us.