I have a productVariant object which has child product object. I want to show the value in the DataGridView, can anyone let me know how to do this?
Here is the structure of the object.
I tried to bind "ProductVariant.Product.Name" to the D开发者_如何学JAVAataProptertyName in the DataGridView, however, it did not not showing any value.
Can anyone help with this? Many thanks.
DataGridView-controls can not bind child objects automatically.
If you are working with Win-forms, my suggestion would be to create two Dialog windows.
One is for parent-objects(with a DGV) and another is for child-objects(with a DGV).
When the user opens the parent window, parent objects would be loaded row-wise in the DGV.
Then if the user double-clicks a row on the parent-window, the child-window would be shown up and the child objects of that parent would be populated on the DGV on the child window.
In this way you can keep things clean no matter how deep the parent-child relationships go. Moreover, add, edit, delete, etc. operations would be much cleaner.
You can also use some third-party controls like, HierarchicalDataGridView.
WinForms binding does not allow traversal of nested properties, though such a thing is possible in WPF. The easiest solution for WinForms is to add a property at the top level (the actual type being bound) that represents what you want to bind--ugly, I know.
You could implement a custom TypeDescriptor
and provide a PropertyDescriptor
that does the same thing. This is the cleanest solution as far as what gets exposed outside of the class, but it's a non-trivial amount of work.
精彩评论