Answer the question
In order to leave comments, you need to log in
How to display multiple string fields and an array of string as columns in a DataGrid?
Hello!
There is a class, it has several fields (for example, of type string) and an array (or List) field.
For example, there are 3 string fields and 2 objects in the array. Then it is necessary to display all this in Datagrid (wpf), in the form of 5 columns.
The bottom line is that I get from the database (MongoDB, although the data itself comes with the API in the form of JSON) several standard fields, and some fields can be added / removed, and I don’t know in advance how many there are in total. Therefore, I find it difficult to write a class in the program and bind it to the table.
It is also possible to change the structure of the database.
The main condition is that users should be able to add or delete object properties, while all current properties should be displayed in the table.
I don't think it's a very rare case. How is it usually implemented?
Broke my head, help please! I will be glad to any advice.
Thanks to.
Answer the question
In order to leave comments, you need to log in
For standard fields, make standard columns in the markup, in the associated viewmodel, make properties, respectively. standard fields, make ordinary bindings for them.
For fields that can be removed / added - generate columns after the data arrives and add them to the datagrid. In the view model, make the UserProperties property of the IDictionary type, and fill the dictionary with the values of non-standard fields. When creating "dynamic" columns, assign them bindings of the form UserProperties[propertyName], so that the datagrid climbs into this dictionary for each element and takes "user" properties from there.
This is an idea, if you need more details - incomprehensible places in the comments. The case is really not rare, I myself recently wrote a very similar thing - data in mongo (due to the lack of any schema, "user" properties), are sent in JSON, it was necessary to show depending on what came.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question