Adding child objects directly in a grid

Adding child objects directly in a grid

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


dtabako posted on Tuesday, April 01, 2008

Hi,

I'm writing my first CSLA app and I have a situation I'm not sure how to handle. I have an editable root object "TaxJurisdiction" which contains a collection of child objects "TaxCodes". Each TaxCode contains properties for 'Tax1', 'Tax2', and 'ItemTax'. Those are values associated with the TaxCode and cannot be changed when building/editing a TaxJurisdiction. What I need is a mechanism to be able to add/remove TaxCodes to/from the TaxJurisdiction. Since I want the user to be able to see the tax fields (but not be able to edit them) my first thought was to make a grid and have the first column (TaxCode) be a comboBox column. The datasource would be my list of TaxCodes, the display member would be the description and the value member would be the Id. The comboBox column would be set to readOnly = false but the other columns would be readOnly = true. This works fine for showing existing TaxCodes within a TaxJurisdiction. The problem comes when you try to add (or edit) TaxCodes within the grid. The comboBox won't dropdown for existing TaxCode objects (maybe because this is the identifier for the TaxCode child object? - anyway, this type of editing of an existing TaxCode is not necessary but would be nice) and if I try to add a new object I get system exceptions that the value in the comboBox is not valid - probably because there is no default TaxCode.

So my question is, is there a good way to do this that I am not getting? Is it even possible? If not - or if it's not the best way to do this - does anyone have any suggestions about what I can and should be doing?

Also, something I haven't yet tried but which I will need to do is display on the form the total for each type of tax (Tax1, Tax2, ItemTax) - which is simply the sum of each type of tax over all selected tax codes. Of course, these totals will need to keep themselves updated each time a TaxCode is added or removed. But they will be display-only values which will not need to be edited or saved back to the database.

Any help would be greatly appreciated.

Thanks, Dennis

rsbaker0 replied on Tuesday, April 01, 2008

You didn't mention whether this was a WinForms application or not. If it is, you can subscribe to the AddingNew event of the BindingSource object you are using for your TaxCodes grid. This allows you to create the "new" object yourself when the grid needs one, and you can initialize the non-editable properties from the parent object.

dtabako replied on Wednesday, April 02, 2008

It is a WinForms app. But I guess the real problem is that there is no default value to load. What really should happen logically - if this is to be done in a grid - is that the user adds a blank row and then picks a TaxCode from a comboBox in the first column.

I'm beginning to see that trying to use the grid directly here is probably not the way to go. I can easily put a comboBox (with the list of available TaxCodes) and an "Add" button on the form. Then I can make all grid columns readOnly or (probably a better idea) label columns. I just thought it might be a little more slick if I could work it so the user could do it all within the grid.

Is there a "best practices" or generally accepted way of doing what I'm trying to do here? Or is it pretty much to each his own?

Thanks, Dennis

rsbaker0 replied on Wednesday, April 02, 2008

Is there a setting for the combobox control you are using that will allow null values? Of course, the object is broken until the user picks one, but that would allow use of a grid.

(We're using a third-party control library and null values aren't a problem for their combo-type control)

Copyright (c) Marimer LLC