Solution for WebHierarchicalDataGrid “DataKeyField is invalid” error

If you’re using Infragistics WebHierarchicalDataGrid and getting “DataKeyField is invalid” error after assigning DataKeyFields property for the root level:

xMyGrid.DataKeyFields = "[Key 1],[Key 2],[Key 3]"

and trying some data manipulation (like deleting rows, binding data etc.), try using GridView property of the grid instead:

xMyGrid.GridView.DataKeyFields = "[Key 1],[Key 2],[Key 3]"

The first method used to work, but somewhere around 2011 release of NetAdvantage it broke.

10 replies on “Solution for WebHierarchicalDataGrid “DataKeyField is invalid” error”

  1. Upon coming across your post, one of our engineers reviewed what you stated in your blog post. What they noticed is:

    xMyGrid.DataKeyFields = “Key1, Key2, Key3”

    If the column’s name is actually [Key2] and not [ Key2], then you have to get rid of the space after the comma. The delimiter here is a comma. Spaces are allowed in most databases as part of column names so we do not trim the column name as the space could actually be intentional.

    If you still experience issues after this suggestion, please submit a bug.

  2. Wow, Infragistics staff is on my blog, I am honored 🙂 but the issue isn’t with spaces, I put them in the post code intentionally for better viewing experience and will correct it now. Original issue remains though – in previous versions of .NetAdvantage (9.x I believe) direct assignment to “Grid.DataKeyFields” worked, and in 11.x+ it doesn’t. It’s not so much a bug as breaking code, but it has to state so in the release notes imho.

  3. Yuriy,

    We attempted to reproduce the issue that you are correcting with this blog post. In our testing we didn’t reproduce the issue. Would you be able to send me more details through email so that I could look into the change in behavior that you saw?

    Developer Support Supervisor

  4. Alan, thank your for your interest but I don’t believe this is an issue you should concentrate your effort upon. If someone experienced it – they can use this method as a workaround (which technically should be used in the fist place). I’d rather you improve general WHDG stability & performance.

  5. I have the same issue but with the WebDataGrid which doesn’t have a .GridView property to access. Is there an equivalent in the WebDataGrid?

  6. @David: WebDataGrid doesn’t have a GridView, because unlike WHDG it hosts a single grid, so all the properties should be available on the WebDataGrid object itself

  7. Hi Yuriy,

    Thank you for your post. I have a WHDG with a drop down that will allow the user to dynamically change the Parent columns. The child report is a list of projects that employees are working on. The parent level can either be manager or project. Each parent selection would work the first time I opened the report, but if I ran one and then changed to the other, I was getting the “Datakey Field is invalid” message. Setting the DataKeyFields attribute at the WebGrid level and the GridView level, as you suggested, did the trick.

    WebGrid1.DataKeyFields = “ManagerName”
    WebGrid1.GridView.DataKeyFields = “ManagerName”

    Thanks again. I wish I had read your post more carefully the first time I saw it! I spent about 4 hours of trial and error until I read your post the second time.

  8. @Kathy I know the feeling, been there myself. Glad this little post was helpful.

  9. Pay attention to the case of your fieldnames! I had a field in my dataset named “idStudent” and I entered “IdStudent” in the key field. It took me a few hours to figure it out.

Leave a Reply

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