Add a checked property to a read-only object

Add a checked property to a read-only object

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


JonM posted on Monday, July 13, 2009

I have a read-only list that needs a checkbox when bound to a grid. Actually this comes up a lot. I don't need to save the checkbox state I just need it in-program use. Any ideas?

JoeFallon1 replied on Monday, July 13, 2009

I do this all the time in my web app. In fact in many cases I do need to save the state of the checkbox. But for some screens I just need to know which rows the user checked so I can process only those rows instead of all of them. So I modify my Read Only Collection and Info object to have a Read Write property for the check box. The ROC now knows the state of the checkbox but does not bother saving it to the database. When the ROC is done processing the rows that have check=True, it goes out of scope and takes the checkbox data with it.

Joe

 

JonM replied on Tuesday, July 14, 2009

Do you use one of the newer 3.5x managed backed properties or just a regular .net property?

lukky replied on Thursday, July 16, 2009

Joe,

I've had such a scenario where I needed to add a "Quantity" property to an "Info" object so that user could enter the quantity it in a WinForm DataGridView. The Quantity property is never persisted, it's only used for processing.

So I've tried to naively add a decimal read/write property to the "Info" object, but somehow the DataGridView won't allow me to edit it. If I change the "List" object to inherit from BusinessListBase and also the Info to inherit BusinessBase , then of course the grid allows editing.

I had also tried to set the IsReadOnly property on the List to false, but that wouldn't allow editing in the grid. I'm assuming there's some other property on the list class that I need to change.

What did you have to do to get it to work ?

Regards.

JoeFallon1 replied on Thursday, July 16, 2009

Luc,

You may be relying on DataBinding. Which you just proved won't work. I just use code to bind to the grid and unbind from the grid to set the sole read/write property.

Joe

 

lukky replied on Monday, July 20, 2009

JoeFallon1:

You may be relying on DataBinding. Which you just proved won't work. I just use code to bind to the grid and unbind from the grid to set the sole read/write property.



Joe,

You are right. I'm binding the additional properties to columns in the datagridview.

As I said, so far my solution is to derive from BusinessListBase instead. It works, but I wonder if there wouldn't be a way to somehow wrap the list within another object and add the properties to THAT object instead. I wonder what would the minimal interface have to be in order to maintain databinding just for editing purposes (No record adding/deletion is done through the grid).

Regards.

JonM replied on Tuesday, July 21, 2009

The only solution I've found so far is to make the class inherit from businessbase instead of readonly. Then I can add my checked property. I simply don't support a save on this object. It is a little hacky but I'm not willing to add a bunch of code to the UI just to deal with this one column. When I need to process the list I just want to be able to walk through all the items and perform a specific action if the checked property is true.

Fintanv replied on Tuesday, July 14, 2009

Just to add my $0.02 ;-)
Should the RO object even know whether or not it is checked? Isn't this a concern better placed elsewhere? Since I am using the MVVM pattern, I have placed this bit of info in the View Model. It knows whether or not it is checked/selected. The underlying RO object never knows/cares, but you can still view in a data grid and take action based on the state.

ajj3085 replied on Tuesday, July 14, 2009

Probably the most pure way to go, since you may use the RO objects to display other data that DOESN'T require knowing if its selected... or maybe something more advanced, like an ordering. I think I'd prefer your way, making something in the UI whihc knows which object is checked or not.

Copyright (c) Marimer LLC