I am trying to use DataSet and DataAdapter to “filter” and “navigate” DataRows in DataTables.
THE SITUATION:
I have multiple logical objects, e.g. Car, Door and Hinge.
I am loading a Form which will display complete Car information including each Door and their respective Hinges. In this senario, The form should display info for 1 car, 4 doors and 2 hinges for each door.
Is it possible to use a SINGLE DataSet to navigate this Data? i.e. 1 DataRow in car_table, 4 DataRow in door_table and 8 DataRow in hinge_table, and still be able to navigate correctly between the different object and their relations? AND, able to DataAdapter.Update() easily?
I have read about DataRelation but don’t really understand how to use it. Not sure if it is the correct direction for my problem.
If you’re looking for an answer towards using DataSets, this won’t be it. Instead, I would like to offer an alternative suggestion: If possible, don’t use DataSets.
Instead of using a
DataSetfor your scenario, I would write some data provider classes for these logical objects (Car,Door, andHingeeach get their corresponding data provider class:ICarProvider,IDoorProvider,IHingeProvider) and gather the required data from them when you load your form with data.You would then delegate loading of the door data to the
Carclass (since the doors “belong” to the car). Make that class have a collection property,Doors(e.g. of typeICollection<Door>). Inside yourCarclass, you might load door data as follows:Likewise, delegate loading of the hinges data to the
Doorclass, since the hinges seem to logically “belong” to a door. TheDoorclass would then have aHingesproperty (again, e.g. of typeICollection<Hinge>). Again, yourDoorclass might load hinge data as follows:When you then load your form, you simply set your controls’ values to the corresponding properties of your
carobject.P.S.: My answer makes quite a few assumptions about your object model and the relationships between your objects. Feel free to adapt my answer to the object model you’ve actually got.
P.P.S.: You could even go a step further. For your form, define various user controls that represent one door, or one hinge. They can get their data directly from a
DoororHingeclass. Then, create a user control that can initialise itself from anICollection<Door>(or anICollection<Hinge>, respectively), and that creates the right number ofDoor/Hingechild controls inside itself. That way, you should be able to use data binding to load your form data directly from aCarobject.Reply to the first two comments by Jake:
No, I meant something different. What I said was that you can do better than using DataSets. I would personally strive for a solution that does not use DataSets at all. I had something in mind like this:
You would then data-bind your form controls against these objects directly (via
someControlDisplayingCarData.DataSource = someCar;), not against DataSets.