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

Binding to Dictionary

7 Answers 80 Views
PivotGrid
This is a migrated thread and some comments may be shown as answers.
ZooY
Top achievements
Rank 1
ZooY asked on 21 Jan 2013, 11:50 AM
Can I Binding PivotGrid to List<Dictionary<>>?
Or any other objects with unknown struture (dynamic class for example)?

7 Answers, 1 is accepted

Sort by
0
Rosen Vladimirov
Telerik team
answered on 23 Jan 2013, 03:27 PM
Hi,

You cannot use List<Dictionary>. If you want to create properties dynamically, you can use ICustomTypeProvider (to use it you'll have to download our next week's internal build as we have just fixed some issues with it).

Thank you for choosing RadPivotGrid. Do not hesitate to contact us if you have any suggestions, concerns or problems.

All the best,
Rosen Vladimirov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
ZooY
Top achievements
Rank 1
answered on 28 Jan 2013, 08:54 AM
How to know when released this "next week's internal build"?
0
Rosen Vladimirov
Telerik team
answered on 28 Jan 2013, 09:52 AM
Hello,

You can check under your account. The internal build should be available for download later today or tomorrow.

Hopefully this helps.

Kind regards,
Rosen Vladimirov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
ZooY
Top achievements
Rank 1
answered on 01 Feb 2013, 10:21 AM
I downloaded, updated, checked ... does not work. Maybe I'm doing something wrong? ..

public class MyDataRow : DynamicObject, INotifyPropertyChanged
{
    readonly IDictionary<string, object> data;

    public MyDataRow()
    {
        data = new Dictionary<string, object>();
    }

    public MyDataRow(IDictionary<string, object> source)
    {
        data = source;
    }

    public override IEnumerable<string> GetDynamicMemberNames()
    {
        return data.Keys;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = this[binder.Name];

        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        this[binder.Name] = value;

        return true;
    }

    public object this[string columnName]
    {
        get
        {
            if (data.ContainsKey(columnName))
            {
                return data[columnName];
            }

            return null;
        }
        set
        {
            if (!data.ContainsKey(columnName))
            {
                data.Add(columnName, value);

                OnPropertyChanged(columnName);
            }
            else
            {
                if (data[columnName] != value)
                {
                    data[columnName] = value;

                    OnPropertyChanged(columnName);
                }
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

public class MyDataContext : ViewModelBase
{
    private readonly IList<Dictionary<string, object>> _pivotData = new List<Dictionary<string, object>>
    {
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 1, 1, 0, 0, 0) }, { "Price", 10.40 }, { "Quantity", 148 } },
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 2, 1, 0, 0, 0) }, { "Price", 10.99}, { "Quantity", 122 }},
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 3, 1, 0, 0, 0) }, { "Price", 11.24}, { "Quantity", 80 }},
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 4, 1, 0, 0, 0) }, { "Price", 11.24}, { "Quantity", 90 }},
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 5, 1, 0, 0, 0) }, { "Price", 11.14}, { "Quantity", 140 }},
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 6, 1, 0, 0, 0) }, { "Price", 10.89}, { "Quantity", 162 }},
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 7, 1, 0, 0, 0) }, { "Price", 10.89}, { "Quantity", 181 }},
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 8, 1, 0, 0, 0) }, { "Price", 10.88}, { "Quantity", 180 }},
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 9, 1, 0, 0, 0) }, { "Price", 11.00}, { "Quantity", 116 }},
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 10, 1, 0, 0, 0) }, { "Price", 10.99}, { "Quantity", 128 }},
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 11, 1, 0, 0, 0) }, { "Price", 10.95}, { "Quantity", 145 }},
        new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 12, 1, 0, 0, 0) }, { "Price", 10.45}, { "Quantity", 189 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 1, 1, 0, 0, 0) }, { "Price", 5.22}, { "Quantity", 100 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 2, 1, 0, 0, 0) }, { "Price", 5.99}, { "Quantity", 85 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 3, 1, 0, 0, 0) }, { "Price", 6.04}, { "Quantity", 80 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 4, 1, 0, 0, 0) }, { "Price", 6.28}, { "Quantity", 72 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 5, 1, 0, 0, 0) }, { "Price", 6.12}, { "Quantity", 99 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 6, 1, 0, 0, 0) }, { "Price", 6.59}, { "Quantity", 40 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 7, 1, 0, 0, 0) }, { "Price", 6.29}, { "Quantity", 68 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 8, 1, 0, 0, 0) }, { "Price", 5.99}, { "Quantity", 95 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 9, 1, 0, 0, 0) }, { "Price", 5.89}, { "Quantity", 120 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 10, 1, 0, 0, 0) }, { "Price", 5.99}, { "Quantity", 105 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 11, 1, 0, 0, 0) }, { "Price", 5.96}, { "Quantity", 111 }},
        new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 12, 1, 0, 0, 0) }, { "Price", 5.99}, { "Quantity", 108 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 1, 1, 0, 0, 0) }, { "Price", 22.86}, { "Quantity", 88 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 2, 1, 0, 0, 0) }, { "Price", 23.02}, { "Quantity", 95 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 3, 1, 0, 0, 0) }, { "Price", 23.22}, { "Quantity", 102 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 4, 1, 0, 0, 0) }, { "Price", 21.99}, { "Quantity", 95 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 5, 1, 0, 0, 0) }, { "Price", 22.45}, { "Quantity", 84 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 6, 1, 0, 0, 0) }, { "Price", 22.56}, { "Quantity", 96 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 7, 1, 0, 0, 0) }, { "Price", 22.88}, { "Quantity", 88 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 8, 1, 0, 0, 0) }, { "Price", 22.42}, { "Quantity", 99 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 9, 1, 0, 0, 0) }, { "Price", 22.56}, { "Quantity", 111 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 10, 1, 0, 0, 0) }, { "Price", 22.18}, { "Quantity", 102 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 11, 1, 0, 0, 0) }, { "Price", 22.93}, { "Quantity", 105 }},
        new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 12, 1, 0, 0, 0) }, { "Price", 22.89}, { "Quantity", 122 }}
    };
     
        
    IEnumerable<MyDataRow> _data;
    public IEnumerable<MyDataRow> Data
    {
        get
        {
            if (_data == null)
            {
                _data = new List<MyDataRow>(from i in _pivotData select new MyDataRow(i));

                OnPropertyChanged("Data");
            }
            return _data;
        }
    }
}

<UserControl xmlns:pivot="http://schemas.telerik.com/2008/xaml/presentation/pivot"  x:Class="RadControlsSilverlightApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

    <UserControl.Resources>
        <pivot:LocalDataSourceProvider x:Key="LocalDataProvider">
            <pivot:LocalDataSourceProvider.RowGroupDescriptions>
                <pivot:PropertyGroupDescription  PropertyName="Name"/>
            </pivot:LocalDataSourceProvider.RowGroupDescriptions>
            <pivot:LocalDataSourceProvider.ColumnGroupDescriptions>
                <pivot:PropertyGroupDescription  PropertyName="Date"/>
            </pivot:LocalDataSourceProvider.ColumnGroupDescriptions>
            <pivot:LocalDataSourceProvider.AggregateDescriptions>
                <pivot:PropertyAggregateDescription PropertyName="Quantity"/>
            </pivot:LocalDataSourceProvider.AggregateDescriptions>
        </pivot:LocalDataSourceProvider>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <pivot:RadPivotGrid DataProvider="{StaticResource LocalDataProvider}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
        <pivot:RadPivotFieldList VerticalAlignment="Stretch" HorizontalAlignment="Stretch" DataProvider="{StaticResource LocalDataProvider}" Grid.Column="1" />        
</Grid>
</UserControl>

0
Rosen Vladimirov
Telerik team
answered on 01 Feb 2013, 10:59 AM
Hello,

I'm sorry, but it looks like there is a misunderstanding. When I said, that you'll be able to use "it" from the next week, by "it" I meant ICustomTypeProvider. If you want to use dynamic properties, you should do it with ICustomTypeProvider. More information about this interface can be found here. As previously declared - with the current release you are not able to use Dictionary for ItemsSource.

Once again sorry for the misunderstanding. Do not hesitate to contact us if you have any problems or concerns.

Kind regards,
Rosen Vladimirov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Pavel
Top achievements
Rank 1
answered on 04 Feb 2013, 07:57 AM
I tried to use ICustomTypeProvider, use CustomTypeHelper by your link.
Changed the code as follows:

public class MyDataContext : ViewModelBase
{
        ...
        ObservableCollection<MyDataRow> _data;
        public ObservableCollection<MyDataRow> Data
        {
            get
            {
                if (_data == null)
                {
                    _data = new ObservableCollection<MyDataRow>();
                    MyDataRow.AddProperty("Name", typeof(string));
                    MyDataRow.AddProperty("Date", typeof(DateTime));
                    MyDataRow.AddProperty("Price", typeof(decimal));
                    MyDataRow.AddProperty("Quantity", typeof(int));
                    foreach (var sourceItem in PivotData)
                    {
                        var dataItem = new MyDataRow();
                        foreach (var si in sourceItem)
                            dataItem.SetPropertyValue(si.Key, si.Value);
                        _data.Add(dataItem);
                    }
                    OnPropertyChanged("Data");
                }
                return _data;
            }
        }
        ...
}

public class MyDataRow : CustomTypeHelper<MyDataRow>
{
}

Still does not work.
0
Rosen Vladimirov
Telerik team
answered on 04 Feb 2013, 03:56 PM
Hi Pavel,

Did you download our latest internal build? Using ICustomTypeProvider is available only if you are using it. I'm sending you a project where if you click the button you will see two RadPivotGrids and both of them have some dynamic properties. You can use the implementation for your reference.

Hopefully this helps.

All the best,
Rosen Vladimirov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
PivotGrid
Asked by
ZooY
Top achievements
Rank 1
Answers by
Rosen Vladimirov
Telerik team
ZooY
Top achievements
Rank 1
Pavel
Top achievements
Rank 1
Share this question
or