MVVM How to create a ViewModel for Datagrid, which columns are selected by the user

So, when I have a Datagrid that I want to populate with data from my database. I usually create a ViewModel with the data I wanted to display. But how do I create a ViewModel for my Datagrid when I don’t know what data it will display in it?

So, when I allow the user of my application to specify the columns from the database table that he wants to display in Datagrid. How do I create a ViewModel for this case?

Therefore, I hope that it is clear what I want to do.

thanks in advance

+4
source share
3 answers

Two spring solutions for the mind.

  • Have a ViewModel to store all the columns and just select the user in the view. This is not perfect.

  • Use the reflection of the View data to define the columns for the ViewModel.

+2
source

I have done this before as follows:

  • an XML document is used that describes all available columns, their formatting (ie "# 0. # 0" for numeric columns) and the data field to which they are bound also describes which columns are the standard ones

  • the description of this XML column is retrieved by calling WCF on the server (it was a Silverlight project)

  • grid columns are generated by the view using the factory grid column (this is done using the view because it is highly dependent on the user interface). Columns are set to visible by default, the rest are set to invisible.

  • The ViewModel is populated with a list of data objects containing all the data for the columns.

  • the user can right-click on the grid and use the context menu to call up a dialog containing a complete list of columns with a tick mark (check) next to those that are currently visible. This dialog is controlled / populated from the view - since the ViewModel does not need to know anything about the actual interface. You can use the dialog service for this. The VM dialog is populated with a list of anonymous objects containing column headers and column visibility status.

  • when the user closes the dialog box, the view will show / hide the corresponding columns (I went further - when the user checks the column in the dialog box, it instantly becomes visible in the grid, so that the user knows that they have selected the correct one, when many tens of columns are displayed, many can be very similar). A.

The advantage of using an XML document to describe columns:

  • he is an agnostic. it doesn't matter which component of the grid i use all i need to do is change the factory

  • An XML document can be updated separately for code; if the client wants to change the format string, I do not need to recompile the code. Similarly, if I need to show more data in the future, all I need to change is my XML document, the assembly containing the definitions of the data objects, and the DAL - V / VM node does not need to be changed at all.

+2
source

Do you even need to create a presentation model? Can't you just populate a local DataTable and bind a DataGrid to it? There are a lot of functions baked in ADO objects, and the DataGrid interacts with a lot of it. If you really need additional properties in the DataRow (for example, commands, for example), you can subclass DataTable and DataRow and add them. This can save you a lot of work.

Whether you are creating a DataTable or creating your own class, a fairly simple approach is to set AutoGenerateColumns to true and handle the DataGrid.AutoGeneratingColumn . You can intercept the list of columns that you want to hide, and then set e.Cancel to true when the generated column is in this list. The documentation shows an example of how this looks.

0
source

All Articles