C# – Trying to figure out how to allow a user to edit a collection with a DataGrid

ccollectionsdatagridienumerable

I'm working on a plug-in for a 3D modeling program to assist in architectural design. I have a Building class which needs to contain a collection of Floors.

My Floor class has properties such as Elevation, Height, ProgramType (residential, retail, etc), and ID. Each building has a FloorList property which is the collection of all the Floors contained by the Building. The properties of a Building are generally viewed through a property grid control and I was planning on using a custom UITypeEditor to allow the user to edit the FloorList collection.

I'm trying to figure out the best way to create this FloorList collection class so that creating the UITypeEditor is as easy as possible, and I can take advantage of any existing collection editing ability's in the .NET controls. For the editor I was thinking I'd use a DataGrid view to show all the floors in the collection something like the table below.

Height    Elevation     ProgramType    ID
15'       70'           Residential    23423
15'       55'           Residential    42342
15'       40'           Residential    98723
20'       20'           Retail         23454
20'       0'            Retail         98723

The user should be able to edit the height of the floor through the DataGrid, but the elevation of a floor would be derived from the sum of the floor heights below it. The program type should either be a text box or a drop down, and the ID is a read only value shown for information only.

The interface should also allow the user to edit the members of the collection such as adding one or more floors, removing one or more floors, or changing to order of the floors.

The DataGrid control has some built in functionality for allowing a user to add and delete rows, but is there built in functionality to allow a user to reorder the rows?

The only times I've ever used a DataGrid has been to show a DataTable. Is there a straight forward way to use a DataGrid control to allow a user to see and edit properties of objects in a collection? What about being able to add and remove objects from that collection by adding or removing rows from the DataGrid?

One caveat to this problem I have is I can't just edit the collection items directly, as changes to one item will affect another. For example if I remove a floor I'll need to move down (change the elevation of) any floors above the removed floor. Likewise if I increase the height of a floor I'll need to move any floors above the taller floor up. So any changes to the collection or items in the collection will need to be done through the collection itself rather then directly at the item.

So again I was thinking my FloorList collection would implement IEnumerable so that other methods could itterate over all the floors in a building. But any actual manipulation of the collection or objects in the collection would be done through Methods of the collection such as….

FloorList.AddFloor(Floor floorToAdd);
FloorList.RemoveFloor(Floor floorToRemove);
FloorList.MoveFloor(Floor floorToMove,int newPosition);
FloorList.ChangeFloorHeight(Floor floor);
etc..etc...etc..

Sorry for the long question, especially with out any sample code. But right now I don't really have any code written as I'm for once trying to plan out my work rather then jumping right into the code.

Update

I've been reading some examples and it looks like the DataGrid will show properties of objects in an IList. So now I'm thinking it makes more sense to implement a List, but set up the overridden methods to handle changing the floor elevations. If I'm ultimately going to be binding this list to a DataGrid control would it be better to base the FloorList collection of BindingList or IBindingList instead?

Update

I've done some more reading and decided to completely reword my question. The new version is here.

Best Answer

To answer your first question: In your business layer I would have Create, Add, Edit, Delete (and so on) methods that handle all your custom logic (just have it pass in your models to these methods). Then call those methods through the OnEditing, OnUpdating, etc. events from the DataGrid.

To answer your second question, BindingList will give you a built in OnChange event whereas the List will not. If this is being used in a Windows Form app, BindingList will come in handy. But using that event in a Web app can get kinda hairy. It depends on how you want to implement the updating logic in your business layer.

Related Topic