Binding EditableRootListBase objects to DataGridView

Binding EditableRootListBase objects to DataGridView

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


Bob posted on Thursday, September 27, 2007

I have a collection of objects based on EditableRootListBase each object being based on BusinessBase.

I retrieve a collection from the db and bind it to a data grid view. On editing I add a row which violates the business rules and the errors show on the dgv margins as the should.

However if I advance off the row with the invalid data the Save() method is called on the object which then throws an unhandled exception because IsValid is false.

I have tried to couple the object IsValid property to the dgv's validating and rowvalidate events but the object Save method seems to be called before the events.

Of course I can override the Save method and only call the base method if IsValid is true but then the grid allows me to keep on adding several invalid rows.

Obviously I'm doing something very wrong. Any ideas what?

 

Bob

 

tetranz replied on Thursday, September 27, 2007

I think this has come up before and there is no perfect answer. I haven't used DataGridView for a while so I don't remember if it shows errors exposed via IDataErrorInfo.

I'm using the Janus grid and I override SaveItem in the collection so that it only attempts to save if the child is valid. That works pretty well because the user sees the red exclaimation points indicating errors and they know that anything with an error has not been saved.

Ross

Tray replied on Sunday, September 30, 2007

Hi Bob,

The DataGridView does have a DataError event which is fired if the bound object throws an exception when the control calls code in the object (in your case, the Save method on the object is throwing an exception when it tries to save an invalid object).
If you use this event, you can catch the exception and handle it (maybe popping up a messagebox to the user to inform them that the item is not valid and cannot be saved and displaying the errors). Check out http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.dataerror.aspx.

If you want to be able to leave the invalid object in the DGV, you may want to follow tetranz idea. This would be a perfectly usable solution. Note that the user may think that they have been able to save an invalid object, which is not the case.

Hope this helps
Tray

jhw replied on Sunday, September 30, 2007

I have tried both ways, handling datarow error and overriding save to handle invlalid state. In the end I went with handling the datarow error. If the object throws another error(like a database constraint), then it will be an unhandled exception unless you handle the dataerror event.

 

Heath

Devi replied on Tuesday, March 25, 2008

I am also using a DataGridView with EditableRootListBase class. And used DataError event of DataGridView. While deleting it catches an exception related to database. Till this event it working properly..

But after that when I clicking another row in the Grid, it shows an DataPortal.Update Error while working DataPortal.Delete method. This method working with the same data that of I clicked first time in the grid for deleting....

 

Copyright (c) Marimer LLC