This is a migrated thread and some comments may be shown as answers.

Binding to objects containing lists of key/value pairs

1 Answer 696 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kasper Schou
Top achievements
Rank 1
Kasper Schou asked on 27 Aug 2011, 10:13 PM
Hi Telerik,

I'm developing an application using your RadGridView to display a list of production orders with a selection of data. Now I'm facing an issue where some values on the order cannot be bound at compile time, since they can differ depending on the production site that loads the list of orders. For keeping things as simple as possible, consider the following objects that needs to shown in a gridview:
public class SiteOrderHeader
{
    public int SiteOrderId { get; set; }
    public List<MachineSpeed> MachineSpeeds { get; set; }
}
 
public class MachineSpeed
{
    public string Type { get; set; }
    public int Speed { get; set; }
}

Now, the number and values of MachineSpeed is unknown at compile time, but lets assume that during runtime a SiteOrderHeader would be loaded with the following data:

SiteOrderHeader siteOrderHeader = new SiteOrderHeader();
siteOrderHeader.SiteOrderId = 1;
siteOrderHeader.MachineSpeeds.Add(new MachineSpeed() { Type = "Type1", Speed = 10000 });
siteOrderHeader.MachineSpeeds.Add(new MachineSpeed() { Type = "Type2", Speed = 6000 });
siteOrderHeader.MachineSpeeds.Add(new MachineSpeed() { Type = "Type3", Speed = 12000 });


This should result in the following grid layout, where the MachineSpeed types are columns and the speed is the value.
SiteOrderId Type1 Type2 Type3
1 10000 6000 12000
      
All SiteOrderHeader that should be shown in the same grid will have the exact same MachineSpeed types but different values, so I could easily add the columns on runtime, but how can I map the speed values to the corrosponding type?

I would prefer if this could be databound in some way during runtime without the need of knowning the MachineSpeed types.
Please guide on suggested way to do this with RadGridView. I can also change the class layout of SiteOrderHeader and MachineSpeed if this could be build in a way that fits into RadGridView, i.e. use a Dictionary<string, int> for the MachineSpeed definition..

Any help would be much appriciated.

Best regards,
Kasper Schou

1 Answer, 1 is accepted

Sort by
0
Nyami
Top achievements
Rank 1
answered on 30 Aug 2011, 09:36 AM

I'm assuming you are using some sort of ViewModel and you know all about DependencyProperties and IValueConverters or IMultiValueConverters.. if you don't then you'll probably have a bit of reading to do!

My first thought would be to start out by creating a custom grid which inherits from the RadGridView, you will need to let it know what the different MachineSpeed types are at run time so create a property on your custom grid which will be used to add the columns to your grid. You would bind this property to a property in your ViewModel that would somehow get the distinct MachineSpeed types. You would then use the PropertyChangedCallback on your MachineSpeed types property of your custom grid to add these columns to the grid. When these columns are added you would the create the binding to the MachineSpeeds property of your SiteOrderHeader object and use a Converter (probably a multi value converter so you can use the ConverterParameter so you know which type the column is for) to display the correct text value.

This method would ensure you have a reusable control (assuming you are binding the ItemsSource to objects that have the MachineSpeeds property that you would bind to) that is easy to bind to and you’ll get all the good stuff from the RadGridView.

Hope this helps…

Tags
GridView
Asked by
Kasper Schou
Top achievements
Rank 1
Answers by
Nyami
Top achievements
Rank 1
Share this question
or