Hello,
In my project, I use Infragistic's XamGrid. My database query result returns me a datatable which I need to bind with Xamgrid's ItemsSource.
MyGrid.ItemsSource = dataTable;
Binding datatable directly to DataGrid is not a problem but I want to convert my datatable to observablecollection. Converting to observable collection will help me in updating the subsequent changes (i.e. Adding/Editing/deleting of Rows from datatable) to UI layer faster. Now the problem is datarow object which is returned to me as part of datatable row collection is not always of the same type. What is returned as datarow depends on what I query and to which datatable in my database.
Because my datarow Item object type is not fixed I'm not able to use the ObservableCollection (because I need to specify the object type while creating the observable collection)
new ObservableCollection<T>(IEnumerable<T>)
I don't know <T> i.e. what is the datarow object type. In such case, is it possible to create the ObservableCollection and bind it to grid?
If using the ObservableCollection is not possible then what are the other ways to update the UI faster with database changes. (In my case per second around 100 rows may get changed/added/deleted).
Thanks,
Hello Krunal,
Thank you for your post. I have been looking into it and the thing I can suggest you is using DynamicObject class, but I am not completely sure that the operations you want to use will be faster if you use ObservableCollection of DynamicObject. Since this data conversion is not related to the XamGrid, I can suggest you ask your question in the MSDN forums,where you will get an answer from an MS expert. Once you get your data converted and you have issues using it with the XamGrid, you can write us and we will be happy to assist you.
Stefan,
Thanks for your reply.
I searched online extensively for the solution to my problem and found the following link provides the solution to my problem :
http://blogs.msdn.com/b/silverlight_sdk/archive/2011/04/26/binding-to-dynamic-properties-with-icustomtypeprovider-silverlight-5-beta.aspx?CommentPosted=true#commentmessage
However, this link provides solution for Silverlight application. WPF adaption of this solution is available at following link :
https://www.develop.com/dynamic-type-binding-in-wpf-45utm_campaigndevelopments-may-2012utm_sourcedevelopments
Both of the above link provides solution by adding properties to object dynamically rather then constructing the whole object as dynamic object. Above solution, works properly with .net framework Grid. (You can download the code from the silverlight link above and check how it is working).
In the WPF solution, I replaced the datagrid with Infragistics XamGrid and was hoping it will work but unfortunately it is not able to get the dynamic properties and show it in grid. It is only able to show the static properties only (i.e. properties defined at design time only)
I also tried creating the collection of ExpandoObject and bind as ItemsSource of XamGrid but it didn't work. You can access my sample code with above experiments with XamGrid at following location :
https://drive.google.com/file/d/0BzF4YuWPqFKRZUlVMVduOXNlN1k/edit?usp=sharing
Can you pls look at the sample code and provide help on how I can achieve the result.
Regards,
Krunal
I have been looking into you sample and I modified it, so now the XamGrid shows the items from the collection with the ExpandoObjects. Basically I defined UnboundColumn for each Property of the ExpandoObject. I also added a method that receives a column's name as string and returns a DataTemplate for the UnboundColumn's ItemTemplate property. Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.
Hi Stefan,
Thanks for your replies and efforts to work on my problem.
It seems like you have provided what I was looking for. But I will have to start using it in my actual application and see whether this concept indeed works for me as intended.
One thing which I need to understand why you have used "UnboundColumn"? Using unboundColumn, will it impact the updates to cell values if data in my source changes?
Looks like I spoke too early. When I tried to put your solution in actual implementation, I realised that though I know the Property Name, this property name is dynamic. Hence, I cannot have fixed property Name for my dynamic object.
One query result could be with two columns
Age | Married
while another query result could be with other two columns i.e.
FirstName | LastName
Hence, I cannot write x.Age = "33" because Age will not always be property of dynamic object. Hope you are getting what I'm trying to say.
Given the situation I think only way to fix this problem is to create dynamic properties and manage to show data for these dynamically added properties in XamGrid and allow binding. Is this possible?