Reshape your Data | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (182).NET Core  (28).NET MAUI  (233)Angular  (117)ASP.NET  (49)ASP.NET Core  (81)ASP.NET MVC  (87)Azure  (42)Black Friday Deal  (1)Blazor  (244)BoldSign  (15)DocIO  (24)Essential JS 2  (110)Essential Studio  (201)File Formats  (74)Flutter  (140)JavaScript  (227)Microsoft  (122)PDF  (86)Python  (1)React  (107)Streamlit  (1)Succinctly series  (131)Syncfusion  (993)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (46)Webinar  (53)Windows Forms  (59)WinUI  (72)WPF  (165)Xamarin  (159)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (11)Business intelligence  (55)Button  (4)C#  (164)Chart  (154)Chart of the week  (62)Cloud  (16)Company  (440)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (74)Development  (694)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (13)Excel  (43)Extensions  (31)File Manager  (8)Gantt  (23)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  (530)Mobile MVC  (9)OLAP server  (2)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (48)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (8)Reporting / Back Office  (9)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (5)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (32)Solution Services  (4)Spreadsheet  (11)SQL  (15)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (426)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (648)What's new  (354)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.


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


· 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;




//return the dynamic data

 public IList DynamicData
                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()
            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.


Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed