Daily Archives: 03/31/2011

WebHierarchicalDataGrid: Locate cell by column name

If you need to get/set value of a specific cell in WHDG Row (which is of GridRecord or ContainerGridRecord type) the only way to do it is by Cell index e.g.

Protected Sub xMyGrid_InitializeRow(ByVal sender As Object, ByVal e As GridControls.RowEventArgs) Handles xMyGrid.InitializeRow
   e.Row.Items(15).Value = 42
End Sub

But what if you don’t know cell/column index and only column name is known? Then the trick is to find the index first. Consider following code:

Protected Sub xMyGrid_InitializeRow(ByVal sender As Object, ByVal e As GridControls.RowEventArgs) Handles xMyGrid.InitializeRow

   Dim oParent as GridControls.ContainerGrid = e.Row.Items.Grid
   Dim iColIndex as Integer

   if oParent.Columns("MY_COLUMN_NAME") IsNot Nothing
      iColIndex = oParent.Columns("MY_COLUMN_NAME").Index
      e.Row.Items(iColIndex).Value = 42
   End If

End Sub

It’s pretty straightforward, first you locate grid to which the row belongs, then locate column in that grid by name and get index of that column and finally locate cell in the row by that index. This method will work at any depth in the WHDG hierarchy.