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.

2 replies on “WebHierarchicalDataGrid: Locate cell by column name”

  1. Hi, I was doing something similar to this, changing the Text property of cells in InitializeRow. But I’ve found it only works the very first time the page is loaded. In my code, I do manual CRUD, so editing causes AJAX to do a postback. When it does the InitializeRow event is triggered again, it goes through the motions, but the changes to Text don’t stick. I’ve debugged and it does change the values, but they seem to be overwritten by “something” after the InitializeRow event handler completes.

    Any ideas?

  2. Sorry for such a late reply. Have you found the solution?
    I wonder why would it trigger InitializeRow for an AJAX call? Or is it a full page postback?

Leave a Reply

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