Reshape your Data | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (203)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (65)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (81)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (897)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (39)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (501)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (381)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (323)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Reshape your Data

Use dynamic objects to reshape data and display it in a WPF Grid

WPF’s binding syntax offers excellent flexibility when binding to complex data. You can craft intricate binding expressions to display data in the desired form. There are, however, times where binding does not quite cut it. You just have to reshape the data in more fundamental ways.

You can reshape the data with ViewModel specific objects and boiler plate code. But, what fun is there in slogging through all that tedious code? With .NET 4 and dynamic objects, you can avoid all that work and reshape your data with little effort.

Essential Grid offers great support for binding to dynamic data. All you have to do is turn on a single property, IsDynamicItemsSource, and you are on to other tasks.

Consider the sample data below. We have a class named Car. It has 3 simple properties, Model, Manufacturer and price. When displayed, a simple collection of cars will appear as below.

clip_image001

Now, assume that your users require the data to be displayed as below.

clip_image003

· Each car is a column instead of a row.

· The column header is formatted with a combination of properties.

· Price is the only element that appears under each column.

There are many ways to achieve this – depending upon the grid you have chosen. In this case, I am going to use Essential grid. Our grids support for dynamic objects will make the job super-easy.

In our ViewModel we build a collection of dynamic objects as below. The code that reshapes the data is highlighted.

ObservableCollection<Car> list = new ObservableCollection<Car>();

ObservableCollection<dynamic> dynamicList = new ObservableCollection<dynamic>();

public ViewModel()

        {

            list.Add(new Car(“Corolla”, “Toyota”, 10001));

            list.Add(new Car(“Accord”, “Honda”, 10002));

            list.Add(new Car(“MDX”, “Acura”, 10007));

            list.Add(new Car(“ES 300”, “Lexus”, 10005));

            list.Add(new Car(“LX 470”, “Lexus”, 10008));

            list.Add(new Car(“Taurus”, “Ford”, 10001));

            list.Add(new Car(“Volt”, “Chevy”, 10003));

            list.Add(new Car(“S60”, “Volvo”, 10004));

            ExpandoObject o = new ExpandoObject();

            foreach (var mc in list)

             {

                IDictionary<String, object> od = (IDictionary<String, object>)o;

                string displayName = string.Format(“{0}-{1}”, mc.Manufacturer, mc.Model);

                od[displayName + “-price”] = mc.Price;

             }

            dynamicList.Add(o);

        }

//return the dynamic data

 public IList DynamicData
        {
            get
            {
                return dynamicList;
            }
        }

I am simply creating ExpandoObjects and assigning values to the dynamic members. This new list can then be directly bound to the grid (works in XAML also).

public MainWindow()
        {
            InitializeComponent();
            ViewModel vm = new ViewModel();
            dataGrid.IsDynamicItemsSource = true;
            dataGrid.ItemsSource = vm.DynamicData;
         }

That’s all there is to it. Point WPF DataGrid to a dynamic object list, set a single property, and you are done.

 

A complete code sample is available for download here.

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed