N
N
Nidere2019-08-26 18:13:29
WPF
Nidere, 2019-08-26 18:13:29

How to display nested datagrid in rowdetails of outer datagrid?

I'm trying to display a large number of objects with different fields in one datagrid.
I decided that in the external datagrid there would be columns with fields common to all objects (ID, Entity ID, Type), and on click, the datagrid would open, displaying all the other fields of the object that I clicked on.
Here's what's there:

<DataGrid x:Name="Components" 
                  AutoGenerateColumns="False"
            ItemsSource="{Binding world.components}">
            <DataGrid.Resources>
                <c:World x:Key="world"/>
            </DataGrid.Resources>
            <DataGrid.DataContext>
                <Binding Source="{StaticResource world}"/>
            </DataGrid.DataContext>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Component ID" Binding="{Binding cID}" Width="100"/>
                <DataGridTextColumn Header="Entity ID" Binding="{Binding eID}"  Width="100"/>
                <DataGridTextColumn Header="Component Type" Binding="{Binding compType.Name}" />
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <DataGrid
                        AutoGenerateColumns="True"
                        ItemsSource="{Binding ElementName=Components, Path=ItemsSource}">
                    </DataGrid>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>

Here's what happens:
5d63f6c17579d565889961.png
In rowdetails, all objects from the collection are displayed at once, but only the one that was selected in the external datagrid is needed

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cyber_roach, 2019-08-26
@Nidere

What you are doing is bad practice, you will completely kill virtualization and this mess of heavy controls in each other will slow down if there is a lot of data.
A simple rule - 2 datagrids per view - is the limit. (including usability)
RowDetails is intended for a little different, display some line of explanatory text, description, statuses, nothing more.
It's not entirely clear what exactly you want, but it seems that here is the solution to your problem: https
://docs.microsoft.com/en-us/dotnet/framework/
...
some more advanced grid with grouping.
Or here is a guide on how to do it on standard elements
https://docs.microsoft.com/en-us/dotnet/framework/...

D
Dmitry Bashinsky, 2019-08-26
@BashkaMen

You have bound to the same collection, maybe you should bind to SelectedItem?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question