We need to build a hierarchical grid in code behind with dynamically created columns, which are known at runtime. We built an application, where the user can select everything he wants to see in the report. He can select products and productgroups, targetgroups (e.g. Men) and facts (e.g. Buyers abs). I prepared a screenshot for you with an example.
The samples for "Self-Referencing Data" or "Hierarchical Financial Data" would be exactly what we need.But in these samples and all other code snippets I found up to know I only saw code based on a list of objects of a certain class with fixed properties. For example there is a class "Employee" with properties "Id", "Name" and "JobTitle". These properties are connected to columns in the grid using the property name as the key. And this is the main problem for us because we have to generate the grid in code behind, after the user has selected what he wants to see. At that point we know which columns and rows we need to generate in the grid.
It was no problem to generate a DataView dynamically with all columns and rows I need, but the grid shows this input as a "flat" table without hierarchies. I don't know how I could implement something like the sample code, using a IList and connecting it to a special columns, which the grid uses to identify parent objects which have child objects?
Does anybody has an idea how I could realize hierarchies not based on objects of a class with fixed properties?
Thanks C.
Hello Yanko,
I must apologize that you haven't heard from me for quite a long time but we had a certain timeline in our actual project.
I took a look at the last sample you prepared for me, but it also didn't help me further because it works with classes (Data and ChildData) with "fix" properties (ID, Name, ChildName, Age).
Meanwhile we found a solution for what we need to do. The attached sample application was a first attempt to test if it would work. In the class MainWindow we create the ColumnLayout in the method wnd_Loaded. For each Respondentgroup (e.g. Male) we create a GroupColumn and for each Fact (e.g. Units abs) we create TextColumns which are added to the GroupColumn. In the method createDictionary we create the data for each Concept in the table. Because we have no class with fix properties we use our class ConceptDict derived from class Dictionary. For Childentries in the table this class has the property subconcepts which is a List of ConceptDict. Instead using property names we use the keys for our entrries in the dictionary. This gives us the flexibility we need.
In the sample everything is created in a straightforeward way, because it just was a test. In our application we have several loops where we create the ColumnLayout and the data dynamicaly according to the Concepts, Respondentgroups and Facts the user has selected.
I hope this will help you further to understand what we intended to do.
If you know an easier way to realize that, please let us know!
Regards Chris
Hello Chris,
I am just checking if you require any further assistance on the matter.
Hi Chris,
Thank you for your reply. I have been looking into your question and I am attaching a modified version of my sample application() where using radio buttons in order to determine under which grouped column to position particular columns in order to make it dynamically.
Could you please explain in more details what is your issue with creating the second column layout? You just need to create a new column layout, add the desired columns and then add it in the Columns collection of the XamGrid as shown in the attach sample application.
It would be great if you attach a runnable sample application(XamGridWPFHierarchyInCodeBehind2.zip) that follows exactly your scenario in order to see what exactly is going wrong.
Looking forward to hearing from you.
I'm sorry but the sample doesn't help me further.
As I tried to explain, we don't have any classes with fixed properties we can connect with columns. After the user selected respondent groups and facts the table might look like the attached file Table_Units_abs_perc.PNG and after changing his selection it might look like Table_Units_abs_perc_Profit_Costs.PNG.
Meanwhile I created at least a "flat grid" to react on the specific selection of a user. In CodeSnippet.cs you will find the method CreateGridLayout, where I dynamically create the GoupColumns and TextColumns. In GetDataforGrid and AddRowsForChildConcepts I dynamically fill a DataView. As I said I just have a "flat grid" now without the possiblity to expand/collapse the shown product groups "Group 1" and "Group 2".
Is there any possibility to achieve this automatically, by providing the data in a different way as I do it?
If not, I would be glad for some support how I could achieve a expand/collapse mechanism.
Regards, C.
Thank you for your reply. Sorry for the misunderstanding. I am attaching the sample application(XamGridWPFHierarchyInCodeBehind.zip) for WPF.
Let me know, if you have any other questions on this matter.