Dynamic DataGrid columns in WPF DataGrid based on the underlying set of data (and their type)

Posted by StatsMan on Stack Overflow See other posts from Stack Overflow or by StatsMan
Published on 2010-05-28T07:26:09Z Indexed on 2010/05/28 7:32 UTC
Read the original article Hit count: 1595

Hello everyone,

I've got kind of a conceptual question. I am in the process of wrapping some statistics classes I wrote into WPF.

For that I have two DataGrid(-Views, currently in WinForms). In one DataGrid each row represents a column in the other. There I can set-up different variables (as in mathematical/statistical variables) with fields like "Header", "DataType", "ValidationBehaviour", "DisplayType". There I can also set-up how it should be displayed. Some Columns can automatically be set to ComboBoxColumns, some TextBoxColumns, and so on and so forth.

So, now once I've set-up these Columns I can go to the other grid and enter my data. I may, for instance, have generated (in grid 1) one Column called "Annual Gross Salary" with input of numerical values. Another Column called "Education" with "0=NoEducation", "1=College Level", "3=Universitary" etc. These labels are displayed as text in the combobox and my statistics engine behind then selects the respective value (0-3) for calculations (i.e. ordinal, nominal variables).

Sooo. In WinForms I could basically generate all the columns by hand in code and then add my data in the respective cells/rows. Now in WPF I thought that must be easy to realise. However, yesterday I got started with ICustomPropertyDescriptor which (maybe I was too thick) didn't give me the results I was looking for.

Basically, I just need to be able to dynamically generate columns (and rows) with different Layout, Controls (ComboBox, simple Input, DateTimes) based on the data that I have. But I don't really know how to go about it?

So here in summary:

  • DataGrid 1

    • Purpose is to display columns that have been specified in DataGrid 2
    • In rows, the user can add any kind of data in the rows below the columns that is allowed as to the columns specifications
  • DataGrid 2

    • Each row in this grid represents a column in DataGrid 1
    • Contains fields like Name/Header, DataType, Validation Behaviour, Default Value, Data Formatting, etc.
    • Also contains a function to be able to set-up how it should be displayed. The user can select from, for instance, ComboBoxColumn (and also add the available options), DateTime, normal TextBox, CheckBox etc.
    • After finishing adding a row it will automatically appear as a new column in DataGrid 1

I'd appreciate any kind of pointer into the right direction. Thanks very, very much in advance! :)

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf