WebHierarchicalDataGrid: Manual Load on Demand when bound to DataSet

Even though I have my issues with Infragistics WebHierarchicalDataGrid control, it has some neat features and I found that with some tweaks you can make it work.

Case in point: Manual Load on Demand. If you have hierarchical data structure, it allows you to retrieve only root level data and then when user clicks “Expand” arrow – get additional data on as needed basis:

Manual Load on Demand in action

This is achieved by handling RowIslandsPopulating grid’s event in which you can run a DB query based on parent row data, then manually create a ContainerGrid object bind it to the data and add it to parent row RowIslands collection:

Protected Sub myGrid_RowIslandsPopulating(ByVal sender As Object, ByVal e As ContainerRowCancelEventArgs) Handles myGrid_.RowIslandsPopulating

     e.Cancel = True

     Dim oData as SomeDataType = GetData()
     Dim oChildGrid As New ContainerGrid()

     e.Row.RowIslands.Add(oChildGrid)

     oChildGrid.DataKeyFields = "SOME_ID"
     oChildGrid.Level = e.Row.Level + 1
     oChildGrid.DataSource = oData
     oChildGrid.DataBind()

End Sub

For this approach to work top-level rows need to display “Expand” arrows that user can click. This is done by looping thru all top-level rows in grid’s PreRender event, setting each row’s IsEmptyParent property to true:

Protected Sub myGrid_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles myGrid.PreRender

    If myGrid.DataSource IsNot Nothing Then
        For Each oRow As ContainerGridRecord In myGrid.GridView.Rows
            Row.IsEmptyParent = True
        Next
    End If

End Sub

This should work, but unfortunately if your source for the root level grid is DataSet – it doesn’t.

Expand arrows aren't rendered

The reason – WebHierarchicalDataGrid expects hierarchy in the data source and if DataSet has a single table and no relationship – the “Expand” arrows will never be rendered. So we have to fake it (Great minds think alike, I found this solution in the same time it was suggested by Infragistics support). Once your root DataSet is filled with data, add following code:

oMyDataSet.Tables.Add()
oMyDataSet.Tables(1).Columns.Add("MY_ID", GetType(Integer))
oMyDataSet.Relations.Add(oMyDataSet.Tables(0).Columns("MY_ID"), oMyDataSet.Tables(1).Columns("MY_ID"))

Just substituted “MY_ID” with some real column ID from your root table. The code will create fake relationship letting grid to render “Expand” arrows when running code from above in PreRender event.

24 replies on “WebHierarchicalDataGrid: Manual Load on Demand when bound to DataSet”

  1. WhenI try to expand the row i get “runtime expection child grid descriptors are not available”. can you show me a working example?

  2. When you expand the row – an Async postback is performed and you need to rebind the grid to the data during this postback – the error happens because grid is not rebound.

    If you do not want to rebind during every postback and do it only for Grid’s Async postbacks – you can use method described here to detect Async postback.

  3. Ok. I can expnad if i click the parent row, but i get only the headers for the child grid. what am i missing?

  4. Can you verify that code that retrieves the data for the child actually has some data and not an empty dataset?

  5. Why does this fail on paging. Like say whne I advance to the second page and try to expand teh parent, i get the exception

  6. Are you getting the same exception as above or different one? Are you rebinding your entire grid to the data on every postback – that includes paging as well?

  7. I just set the paging on teh aspx page and I rebind the grid on every postback, but how to make sure it includes paging?

  8. You can put a break point in your rebinding code and see if it fires on paging.

    Also I found out (the hard way) that sometimes if old data remains in the grid on page change – it can cause issues when new data is loaded for the new page. So on paging, you can try clearing old grid data, with something like this function:

    Sub ClearResultsFromGrid()
       With xMyGrid
          For I As Integer = 0 To .Bands.Count - 1
             .Bands.Remove(.Bands(I))
          Next
    
          .Columns.Clear()
          .Rows.Clear()
       End With
    End Sub

    Or, if this is an overkill and causes issues, at least try Rows.Clear()

  9. I get non invocable member webhierarchicaldatagrid.bands cannot be used like a method on line 4.

    Well the parent grid is firing on paging and so i can navigate to the next page, but cannot expand the parent

  10. NO have the “Expand” arrows and my dataset has relations… why?

  11. @Sergio: Are you using manual load on demand? If so, have you manually set Row.IsEmptyParent = True for the parent rows?

  12. No I can still page but childs grids dont expand for pages beyond the first. STill get ‘runtime exception, no child grid descriptors available’ error when trying to expand.
    But 1st page expands

  13. @Dev_help: Did you verify that grid is rebinding on paging? Also did you try to clearing grid data before page change?

    Does the error still happens if you *don’t* expand anything on Page 1 and go straight to page to and expand child there?

  14. Yes, the grid does rebing on paging.
    Even if i don’t expand anything on Page 1 and go straight to page to and expand child there,still shows me same error..
    How can I clear grid data before page change? where do i set that?

  15. For child grids behaviours to work with manual on demand what is to be done.As initial load only loads teh first page of child grid. Subsequent pages are not loaded

  16. @Dev_help Paging on the child levels in this mode is tricky. I believe there was a bug in WHDG that prevented child grids from paging correctly so currently I do not use paging on the child level. If workaround is found or bugfix is released I will post an update.

    Meanwhile you could disable paging for children to display all rows. Or you can actually enable it (and copy paging settings from the top-level grid) – for that just add following lines in RowIslandPopulating event handler before DataBind:

    oChildGrid.Behaviors.CreateBehavior(Of GridControls.Paging)()
    oChildGrid.Behaviors.Paging.Enabled = True
    oChildGrid.Behaviors.Paging.PageSize = myGrid.GridView.Behaviors.Paging.PageSize

    It will display pager, but, as I mentioned – it doesn’t function properly. Maybe you could make it work? If so, please publish your code here.

  17. I get the same problem even for filtering/sorting. It does not sort / filter. even when I have teh paging removed.

  18. @Yuriy
    So do you currently have filtering/sorting for your child grids? We use version 11.1 and I cannot get any behaviours to work. I am told that to when using a DataSource it is necessary to call the OnRowIslandDataBinding for on demand data. But I am not sure what goes in there

  19. @Dev_help Currently we do filtering/sorting at data level (e.g. SQL Query that retrieves the data is filtered by some criteria, sorted by some columns) we do not use filtering/sorting of WHDG itself – for now I am trying to avoid this. Maybe same approach will work for you.

  20. @Dev_help My problem is that whdg1_RowIslandsPopulating method is not getting called even though i am adding the event handler on “OnInit” method. Any advice?

Leave a Reply

Your email address will not be published. Required fields are marked *