locked
Display data in DataGrid when column count and headings are not known at runtime RRS feed

  • Question

  • Hi

    I have a requirement to display tabular data in a WPF GUI and the DataGrid looks to be the natural choice.

    In this case, the number of columns and their headings can vary at runtime. I have looked at a number of articles and code examples but they all seem to assume that the data is fixed in advance and that you can bind your input data to a class with a property for each column.

    Is there a pattern or technique for doing this when you don't know the structure of the data at compile time?

    Thanks

    Patrick

    Tuesday, July 16, 2013 2:12 PM

Answers

  • Hello Patrick!

       There are properties in the Wpf datagrid like "AutoGenerateColumns".

       If you add an ItemSource to the datagrid, the columns will be generated automatically depending on the source object type.

       Check this out!


    • Edited by Jorge_M_P Tuesday, July 16, 2013 4:39 PM changed link
    • Proposed as answer by Lisa Zhu Thursday, July 18, 2013 11:10 AM
    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 7:21 AM
    Tuesday, July 16, 2013 4:38 PM

All replies

  • Hello Patrick!

       There are properties in the Wpf datagrid like "AutoGenerateColumns".

       If you add an ItemSource to the datagrid, the columns will be generated automatically depending on the source object type.

       Check this out!


    • Edited by Jorge_M_P Tuesday, July 16, 2013 4:39 PM changed link
    • Proposed as answer by Lisa Zhu Thursday, July 18, 2013 11:10 AM
    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 7:21 AM
    Tuesday, July 16, 2013 4:38 PM
  • Hi Jorge

    Thanks for your reply but I don't think this covers my exact problem.

    I need to display the contents of 2 files but these files contain an unknown number of columns (and each may contain a different number) and I need to display them in 2 grids side-by-side. Now the link you gave shows how to manually define columns but not how to manually define them at runtime. Is this possible?

    Thanks for your help

    Patrick


    • Edited by Patrick Aspinall Monday, September 23, 2013 8:11 AM text edit for clarity
    Monday, September 23, 2013 8:10 AM