Hi,
I am using EF class objects as grid datasources generated by command "dotnet ef dbcontext scaffold ...", which reside in another .Net Core project of the VS solution.
The parent class looks like:
namespace myDB.Models { public partial class Parent { public Parent() { ParentChild = new HashSet<ParentChild>(); } public int Id { get; set; } //some other properties... public virtual ICollection<ParentChild > ParentChild { get; set; } } }
This basically works fine when I only use a simple table with SingleBand.
Now I would like to also show the parent's children in the grid.
The child class:
namespace myDB.Models { public partial class ParentChild { public int Id { get; set; } public int ParentId { get; set; } public string Name { get; set; } //some other properties... public virtual Parent Parent { get; set; } } }
But in the grid it does not show me the children columns, instead I see 2 columns "IsReadOnly" and "Count". It seems like it has a problem with the HashSet or the ICollection property (which are generated by the .Net Core command by default).
Is there a way to solve this without having to edit the generated classes too much?
Thank you,
best regards
Hi Andrew and Mike,
in a partial class, I added
public virtual BindingList<ParentChild> ParentChildrenForGrid { get; set; } = new BindingList<ParentChild>();
Using this property, it shows the correct child columns in the grid.
Thank you!
Update:
For anyone who is interested, I added the needed changes to my Github project which optimizes the "dotnet ef scaffold" output. https://github.com/DXSdata/DotnetEfDbcontextConverter
The "--winforms" parameter replaces ICollection with IList, and HashSet with BindingList.
HashSet<T> and ICollection<T> are not good classes to use for data sources of bound controls.
I'm frankly surprised that HashSet<T> works at all. I guess the grid and the BindingManager are able to populate data from it because it's IEnumerable. But there will be functionality in the grid that will not work when using these types.
For example, if you turn on the AddNew row in the grid for either the parent or child band in your example, clicking on the AddNew row will raise an error message that the data source does not supporting adding rows.
Try to delete a row and you will get a similar message.
Try showing the grid initially and then adding or removing rows from the data source and the grid will not be notified of these changes and so the new rows will not show up and the deleted rows will still be there.
There may be other cases that do not work, and I would not be surprised if there were unexpected results in other operations.
I strongly recommend using BindingList<T>. That class is specifically designed for data binding.
Hello Daniel,
I have been investigating into the issue that you are reporting, and I have put together a sample class structure that is consistent with the one you have provided, but in doing so, I cannot seem to reproduce the behavior you are seeing. The UltraGrid is handling the hierarchy normally on my end. Perhaps there is another partial “Parent” or “ParentChild” class in your application somewhere?
I was speaking with my colleague and he had also recommended using a BindingList<T> instead of using an ICollection<T> or HashSet<T> for your “ParentChild” hierarchy in this case. I am unsure if this will help you here, though, as HashSet<T> is working on my end.
I have attached the sample project I used to test this. Please test this project on your PC; whether or not it works correctly may help indicate the nature of this problem.
If the project does not work correctly, this indicates either a problem possibly specific to your environment, or a difference in the DLL versions we are using. My test was performed using version 18.2.20182.175 in Infragistics for Windows Forms 2018 Volume 2.
If the project does show the product feature working correctly, this indicates a possible problem in the code of your application. It will help if you can provide a small, isolated sample application that demonstrates the behavior you are seeing.
Or, if this sample project is not an accurate demonstration of what you're trying to do, please feel free to modify it and send it back, or send a small sample project of your own if you have one.
Please let me know if you have any other questions or concerns on this matter.
UltraGridHashSetTest.zip