WebHierarchicalDataGrid binds to data twice

Infragistics WebHierarchicalDataGrid offers a nice ability to custom load-on-demand data via its ContainerGridDataBinding event. It is very useful during paging or displaying grid children – you can make a DB call and provide data a just needed for current page or child.

But it has a drawback – if you need to programmaticaly sort the grid by manipulating SortedColumns collection – grid thinks it needs to rebind the data and is calling ContainerGridDataBinding event handler again thus making it execute DB call again – which is redundant and may hinder performance. In a typical scenario you have your binding code:

Protected Sub xmyGrid_ContainerGridDataBinding(sender As Object, e As GridControls.DataBindingEventArgs) Handles xmyGrid.ContainerGridDataBinding
   e.Cancel = True
   e.DataSource = MakeDbCallToGetCurrentData()
   e.SelectArguments.TotalRowCount = iTotalNumberOfRecords
End Sub

and somewhere else add sorting

Protected Sub xmyGrid_PreRender(sender As Object, e As EventArgs) Handles xmyGrid.PreRender
   xmyGrid.Behaviors.Sorting.SortedColumns.Add(sCol1Key, SortDirection.Ascending)
   xmyGrid.Behaviors.Sorting.SortedColumns.Add(sCol2Key, SortDirection.Descending)
End Sub

Sorting code in the second event handler is causing grid to perform second call to ContainerGridDataBinding. The solution is to move sorting code inside of ContainerGridDataBinding handler:

Protected Sub xmyGrid_ContainerGridDataBinding(sender As Object, e As GridControls.DataBindingEventArgs) Handles xmyGrid.ContainerGridDataBinding
   e.Cancel = True
   e.DataSource = MakeDbCallToGetCurrentData()
   e.SelectArguments.TotalRowCount = iTotalNumberOfRecords

   xmyGrid.Behaviors.Sorting.SortedColumns.Add(sCol1Key, SortDirection.Ascending)
   xmyGrid.Behaviors.Sorting.SortedColumns.Add(sCol2Key, SortDirection.Descending)
End Sub

Since at this point grid already bound to data – columns are already available. And since we’re inside of ContainerGridDataBinding – the call to it is not repeated.

Leave a Reply

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