Unable to cast object XXX to ExpandoObject when using a class that inherits from base class inheriting from DynamicObject

Hello,

My application uses the grid component to display data with a variable number of columns.

To do this, I use a class that inherits from DynamicObject, as explained in the documentation. Everything works correctly when my class inherits directly from DynamicObject.

There are, however, several different grids to display. To optimize the management of variable columns, I inherited two classes from a base class which itself inherits from DynmaicObject:

08- DynamicObject classes.png

But if I use one of these classes, the grid stops working and the following error appears during validation after editing:

Unable to cast object 'TeamCashbox.Models.AccountingDocument' to type 'Sytsem.Dynamic.ExpandoObject'

Here is a screenshot of the exception:

09- DynamicObject cast exception.png

Do you think it is possible to use a child class of DynamicObject in the grid control?


3 Replies

MS Monisha Saravanan Syncfusion Team March 13, 2024 12:16 PM UTC


Hi Gasser,


Greetings from Syncfusion.


Before proceeding further with your requirement kindly share us the below details to validate further at our end.


  1. Share us the entire Grid code snippet.
  2. Share us the video demonstration of the reported issue.
  3. the following error appears during validation after editing -> share us the way you have handled validation at your end.
  4. Share us the Nuget version used at your end.
  5. If possible share us an simple issue replicating sample.


The above requested details will be very helpful for us to validate the reported issue at our end.


Regards,

Monisha



JD Jean Daniel Gasser March 13, 2024 01:30 PM UTC

Hello Monisha,

Please find a sample project below.

To reproduce the error:

  • Choose “Test grid” in the left menu
  • Double-click on a line to edit it
  • Then click on the “Update” button in the grid toolbar
  • This action should invoke the "OnGridActionFailure" function on which I have defined a breakpoint

Thank you in advance for your help

Regards,

J-D Gasser


Attachment: UnableToCastObjectToExpandoObject_24ba4608.zip


SP Sarveswaran Palani Syncfusion Team March 18, 2024 02:47 PM UTC

Hi Jean,

From your query, we were able to replicate the reported issue on our end. After examining your shared sample, we noticed that you're accessing properties of dynamic objects and binding them to strongly typed objects. This is causing some issues during data operations. To address this problem, we recommend using Dynamic or ExpandoObject, which we have provided built-in support for perform CRUD operation, or alternatively, use fully strongly typed objects in the Grid to overcome the reported issue at your end. Please refer to the attached link for more information.

Reference: https://blazor.syncfusion.com/documentation/datagrid/data-binding#dynamicobject-binding

Demo: https://blazor.syncfusion.com/demos/datagrid/dynamicobject?theme=fluent

If you have any further queries, please get back to us.

Regards,
Sarvesh


Loader.
Up arrow icon