简体   繁体   中英

How to build multi-level datagrid

I'm using Entity Framework 6 with my C# WPF application and want to show my data in a multi-level datagrid.

My dataset contains 2 simple entities:

  • Projects: Id, ParentId (nullable), Name
  • Tasks: Id, ProjectId, Name

So every task is attached to one project. And every project can have an optional parent project. This leads to a hierarchical structure of n levels. Corresponding navigation properties are already in place (Project.ParentProject, Task.Project).

  1. Use Tasks as CollectionViewSource and group by Project. With the use of an IValueConverter I was able to iterate through all parents and write it in a single group row like this: 'Project-1 / Project 1.1 / Project 1.1.1'. The disadvantage is that there is no hierarchy in my datagrid and empty projects will not show up.
  2. Use Projects as CollectionViewSource - this will show all projects. But neither figured I out how to build a hierarchy nor how to show the tasks as childs.

  • Project-1
    • Project-1.1
      • Task-100
  • Project-2
    • Project-2.1
      • Task-200
    • Project-2.2

So every project is shown in its hierarchy even if there are no tasks. At a later point, it would be nice to have the option to hide all empty projects and only show projects (and their parent projects) with tasks.

So has anyone a clue on how to do this? Appreciate your input and ideas!

Have a look at RowDetailsTemplate

You can place a new datagrid within this template and it will do what you want to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM