I am binding my UltraGrid to an Entity Framework (nHydrate) data source (System.Windows.Forms.BindingSource). So for examples sake:
ClassA (Band 0) ClassB (Band 1)
I can add rows to Band 0 without any problems using the Fixed Header Row. I am using the AllowAddNew = FixedAddRowOnTop to do this. I am doing the same with Band 1.
However, when I add a new row in Band 1, a new instance of ClassB is created which I can see in the AfterRowUpdate event. However, the relation between ClassA and ClassB is never set and if I try to save the context I get the error:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
If I manually try to set the relationship either by adding CType(e.Row.ParentRow.ListObject, ClassA).ClassB.ToList().Add(<the new ClassB row> ), then I get an extra row in the grid in Band 1.
This is frustrating me as I have spent a lot of late nights trying to work around this. I cannot see any way to proceed with the grid - I hope I am wrong.
Any thoughts?
Thanks
Andez
Hi Andez,
I'm not very familiar with the Entity framework. But it seems to me that the line of code you added here to set the relationship is not correct. You are adding a new row to band 1 instead of using the existing row that is already created.
What you probably need to do is access the ListObject of the AddNew row in the grid and that should give you the data object (Class B) that corresponds to that row in the grid.
I'm not sure how you establish the relationship between elements in the EntityFramework, though, so I might be wrong.
Hi Mike,
I am getting the entity instance from the ListObject in the parent row.
However, I've tried numerous ways of setting the relationship in the entity framework objects, but that spouts the new row in the grid. Will battle on - maybe 2 grids for now. Would be good if you guys tried some of the Entity Framework with Infragistics. Oh it is fun.
Thanks for the reply.
Even when you set the relationship on the entity object child itself, it causes the extra row. Like I said, I have tried many ways and they all cause a new row to apper. There doesnt seem to be a way around it.
I have resorted to using 2 tables to implement this for now - until I have more free time to investigate.
Many thanks,
The code you posted here is getting the ListObject of the parent row and adding a new child row to it. So it's no surprise that you are getting 2 child rows in that case. The grid tells the BindingManager to add a new row to the data source as soon as you click on the TemplateAddRow.
As I said, I don't know how the EntityFramework connects parent rows to child rows, but the ListObject of the child row in the grid should give you a reference to the Class B object in the data source that is already a child of the parent row. So my guess is that what you need to do is something on that object, not on the parent row.