m:n relations with RadListBox

Thread is closed for posting
1 posts, 0 answers
  1. 78C3B137-E2EA-4459-8402-7347F45A7CFE
    78C3B137-E2EA-4459-8402-7347F45A7CFE avatar
    885 posts
    Member since:
    Nov 2003

    Posted 14 Jul 2009 Link to this post

    Requirements

    RadControls version

        2009.2.701.35

    .NET version

        3.5 sp1   

    Visual Studio version

        2008

    programming language

        C#

    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION

    In the database world you will often find situations where elements a related in a m:n fashion.
    One of the commons things are options.
    A product can have some options like, Colors, Materials, and so on.
    YES - these are typically 1:n relations - a car has one color.
    But think about extras - fog lights, seat heating, leather equipment, sport wheels...
    There is a list of options and you can select zero or more of them for your product.
    In the current example I have shipping items - to keep it simple an item has just an ID and a name.
    Further there is a list of option - "Overnight, GPS tracked, insured,..."
    When a users chooses a product he can select zero or more of these options.
    The tables would look like this:
    ShippingItems: ID, Name
    ShippingOptions: ID, Name
    ShippingOptions2ShippingItem, IDShippingItem, IDShippingOption
    The table ShippingOptions is "static" (in our simple sample) which means it has some entries and will not be changed.
    To assign options to an item we must create an entry in the table ShippingOptions2ShippingItems.
    The easiest way to implement this (I guess) would be ADO Entities.

    But to avoid the need of a database I created some classes which provide the application with sample  data.
    The Data is hold in a Session variable - so you can change it -- and when ever you want reset it to default - nothing is persisted.

    The sample:
    We have a grid with some random shipping items. Each item has (random) shipping options.
    This grid allows to edit these items. For simplicity only the shipping options can be changed.
    To provide editing capability for the options I created a usercontrol. This control has two RadListBoxes and two datasources.
    One datasource is for the available items (ShippingOptions in our case). And the second datasource contains the content of ShippingOptions2ShippingItem.

    To avoid strong relationship of the control to the datasources I used two interfaces.
    One is for a pair of one Name and one ID. To use this with any kind of database you can attributes which access your internal members like "ProductID, ProductName" or some kind of fields like this.
    The second interface is for the pair MasterID - DetailID - in this example ShippingItemID is the master (always the same) and ShippingOptionID is the detail (unique).

    Although not very well documented (at the moment - the control is new) the RadListBox provides access to it's items in callbacks (postbacks). So all you have to do is to iterate through the item list in your update handler.
    For each item in the "Selected" Listbox you create a record in your relation table.
    And for each item in the "Available" (not selected) Listbox you ensure that existing relation records with these items are deleted.
    That's all - very simple.

    A last thing about the UserControl provided in this sample.
    Of course I could have implemented a CompositeDataBoundControl or something like this - but this meas handling ViewState....
    Furthermore - also those controls provide only one DataSource - DataSourceID - but we need two of them
    So in any case I had to find a way to provide a second DataSource(ID).
    Since UserControl has no datasource at all I had to provide both of them. This would mean on the other hand that I had to mess around with databinding...
    You know I'm a bit lazy sometimes. Therefore I wanted neither to implement ViewState handling nor databinding.
    The trick - I create two databound controls in my usercontrol - and let them do the job :)

    Last not least the UserControl has some kind of "read only mode" - so I can use it to display the currently selected options in the datagrid.
    Finally - no headings, no "special design" (width, height, minimal height......) - but I guess (another feature of usercontrols) this can be easily done by everyone of you.

    Kind regards - and I hope you enjoy this sample

    Manfred
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.