Datagridview - New Rows Editable, Old Rows Read Only

Datagridview - New Rows Editable, Old Rows Read Only

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


MadGerbil posted on Thursday, January 22, 2009

I'd like to know a good strategy for making each row in a datagridview editable/non-editable based upon the underlying object's IsNew property.  

The thinking behind this is that I'm writing an accounting application and once a payment is "saved" to the database it shouldn't be possible to edit it whereas if it has not been saved the editing of the payment is acceptable.

So in the datagrid view I may have some rows that should never be edited (they are old) but the new rows may be edited until they are persisted to the database at which point they become read-only.

Thanks in advance.

Fintanv replied on Thursday, January 22, 2009

I have had similar situations and have simply used the CanWriteProperty override for this on a per-property basis.  I then protect the property sets with an if(CanWriteProperty(MyProperty.Name)).
If all the editable properties are protected in this way, then you have essentially made the 'row' uneditable when it is not new.

I have taken the authorizer control concept from Csla and made my own version that works with the Infragistics grid (inherit from DataDecoratorBase).  It queries the underlying bound data item using the Csla.Security.IAuthorizeReadWrite to determine whether or not the cell should enter edit mode.  Works a treat.

-- Fintan

Copyright (c) Marimer LLC