DataGridView - Editable Root Collection Trying to Save New Row

DataGridView - Editable Root Collection Trying to Save New Row

Old forum URL: forums.lhotka.net/forums/t/1180.aspx


Bob posted on Monday, September 11, 2006

I have an editable root collection bound to a DataGridView. The user can add new rows in the grid. In Microsoft speak as soon as the user starts to add a new row, the grid adds a new new row. This new new row gets propagated to the collection and when the user hits a save button which calls the collection's Save() method, the validation rules which block the invalid (blank) object being saved throw an exception.

Is there a way of calling the validation system to prevent the blank row from entering the collection in the first place? Or does this validation need to be done independently in the UI class.

Sorry, I'm new to business objects!

 

Bob

 

 

RockfordLhotka replied on Tuesday, September 12, 2006

You can't do what you ask if you want in-place editing in the grid.

If you want, you could make your child object have a public factory method (NewChild) that the UI can call. Then the UI could create a child and allow the user to edit it in a modal dialog. Only if the resulting object has IsValid=true would you then add it to the collection.

But in-place editing in the grid, through data binding at least, requires that the object be added to the collection before the user gets to interact with it at all.

Another possible solution is to remove the child from the collection if it is not valid, and once the user leaves the row. In CSLA 2.1 (soon in beta) there's a new notification mechanism by which a child notifies its parent when it has completed ApplyEdit. This is through the new IParent interface, exposed as EditChildComplete (a protected virtual method) in BusinessListBase. Using this mechanism, you can be notified when a child has completed editing - and you should be able to check IsValid and remove it if you dislike it.

bmorris replied on Wednesday, September 13, 2006

Thanks very much. At the moment I'm following the first part of your para 3 and removing the invalid child before saving. It does seem a bit of a crude way to do it though. I'll look out for 2.1.

 

Regards

Bob

bmorris replied on Wednesday, September 13, 2006

The way I do this outside of CSLA is:-

Suppose an object Thing which has two public methods void Save() and bool Validate. Things<Thing> inherits from BindingList<T>. Things also have Save and Validate methods which just loop through the child Things calling the same methods

Things<Thing> is bound to the datagridview. The grid's RowValidating event calls the Validate method on the databound object. If the object is valid then the RowValidated event calls the Save method on the databound object. This is a scenario in which the grid saves as it goes and theres no need for a catch all save button, so the newrow never gets saved. I would imagine most people do something of the sort.

The problem is that at the end of the edit Things<Thing> contains at least one juvenile delinquent and if the collection's Save method ever did get called then kerboom.

So it's not a rhetorical question if I ask what's the likely implication of fiddling and using a CSLA child object in this manner. Would it constitute child abuse?

 

Regards

Bob

Copyright (c) Marimer LLC