Click here to Skip to main content
15,349,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display a table format that has multiple columns and rows. The Number of columns and the Number of rows are not static they are decided at the run time as in how many columns and how many rows are present. These are again grouped to show similar type of data.
Let us take an example that I have a employee data and a manager data along with the project
now I want the mapping to be as below
  Manager1     Team            Avg Hours 
     Employee1  <development>      <8>
     Employee2  <development>      <9>
     Employee3  <testing>          <7>
     Employee4  <testing>          <10>
     Employee5  <development>      <8>
     Employee6  <testing>          <8>
   Manager2      Team            Avg Hours  Weekends
     Employee7  <support>           <8>       <yes>
     Employee8  <support>           <9>       <yes>
     Employee9  <support>           <7>       <yes>
     Employee10  <testing>          <10>      <yes>
     Employee11  <development>      <8>       <yes>
     Employee12  <support>          <8>       <yes>

AS you can see the Above list has a grouping and the Columns also are different for each group and all the above data is obtained at the run time
All the data that is in angular brackets is the data that keeps updating and the other do not frequently change. I am trying to bind a List view to a dynamic list of Columns and rows. So here I want the Data Template to be Dynamic to accommodate the Dynamic number of columns
ManagerCollection is a ObservableCollection

What I have tried:

<DataTemplate x:Key="EmployeeTemplate" DataType="{x:Type model:Employee}">

      <StackPanel  Orientation="Horizontal" HorizontalAlignment="Stretch">
      <TextBlock Text="{Binding EmployeeName}" Style="{StaticResource TextBlockStyle}" Width="100" Margin="5"/>
      <TextBlock Text="{Binding EColumn1}" Style="{StaticResource TextBlockStyle}" Width="100" Margin="5"/>
      <TextBlock Text="{Binding EColumn2}" Style="{StaticResource TextBlockStyle}" Width="100" Margin="5"/>
      <TextBlock Text="{Binding EColumn3}" Style="{StaticResource TextBlockStyle}" Width="100" Margin="5"/>
      <TextBlock Text="{Binding EColumn4}" Style="{StaticResource TextBlockStyle}" Width="100" Margin="5"/>
      <TextBlock Text="{Binding EColumn5}" Style="{StaticResource TextBlockStyle}" Width="100" Margin="5"/>
  <HierarchicalDataTemplate x:Key="ManagerTemplate" DataType="{x:Type model:ManagerSet }" AlternationCount="2" ItemsSource="{Binding EmployeeCollection}"  ItemTemplate="{StaticResource EmployeeTemplate}">
      <ListView >
          <ListView.View >
              <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="{Binding MeasurementSet }">
                  <GridViewColumn Header="{Binding ManagerNameName}" Width="120" DisplayMemberBinding="{Binding ManagerName}" />
                  <GridViewColumn Header="{Binding MColumn1}" Width="120" DisplayMemberBinding="{Binding Path=MColumn1}" />
                  <GridViewColumn Header="{Binding MColumn2}" Width="120" DisplayMemberBinding="{Binding Path=MColumn2}" />
                  <GridViewColumn Header="{Binding MColumn3}" Width="120" DisplayMemberBinding="{Binding Path=MColumn3}" />
                  <GridViewColumn Header="{Binding MColumn4}" Width="120" DisplayMemberBinding="{Binding Path=MColumn4}" />
                  <GridViewColumn Header="{Binding MColumn5}" Width="120" DisplayMemberBinding="{Binding Path=MColumn5}" />

<TreeView ScrollViewer.VerticalScrollBarVisibility="Auto" BorderThickness="0" Width="{Binding ActualWidth, ElementName=TreeViewItem }" AlternationCount="2"  ItemContainerStyle="{DynamicResource TreeViewItemStyle}" ItemsSource="{Binding ManagerCollection}" ItemTemplate="{DynamicResource ManagerTemplate}" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
Updated 17-Feb-21 2:35am

1 solution

Maybe you can do something like in this CodeProject example:
Grid with Dynamic Number of Rows and Columns - Part 1[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900