Search code examples
c#wpfdata-bindingdatagridrowdetailstemplate

Binding datagrid to two different classes with lists?


It is my first question on StackOverflow so I hope I am doing nothing wrong ! Sorry if it is the case ! I need some help because I can not find the solution of my problem. Of course I have searched everywhere on the web but I can not find it (can not post the links that I am using because of my low reputation :( ). Moreover, I am new in C# and WPF (and self-learning). I used to work in C++/Qt so I do not know how everything works in WPF. And sorry for my English, I am French.

My problem

My basic classes are that an Employee can use a computer. The id of the computer and the date of use are stored into the class Connection. I would like to display the list information in a DataGrid and in RowDetailsTemplate like here :

enter image description here

So it will do a binding to the Employee class but also to the Connection class with only the last value of the property (here the last value of the list "Computer ID" and the last value of the list "Connection Date" on this last computer). So it is a loop in the different lists. How can I do it ?

Is it too much to do ? :( I succeed to get the Employee informations but I do not know how to bind the list of computer. When I am trying, it shows me "(Collection)" so it does not go inside the list :(

Summary of Questions

  1. How to display/bind a value from a list AND from a different class in a DataGrid ?
  2. How to display all the values of a list into the RowDetailsTemplate ?

Under Windows 7 and Visual Studio 2010 Pro version.


Solution

  • I sugest you familiarize yourself with the MVVM pattern in case you haven't yet... This is pretty basic, so you'd want to modify/style it to get exactly what you want ^_^...

        <DataGrid ItemsSource="{Binding Employees}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
                <DataGridTextColumn Header="Gender" Binding="{Binding Gender}"/>
                <DataGridTextColumn Header="Last computer used" Binding="{Binding LastComputerUsed}"/>
                <DataGridTextColumn Header="Last connection date" Binding="{Binding LastConnectionDate}"/>
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <DataGrid ItemsSource="{Binding ComputerIDs}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Computer ID" Binding="{Binding ID}"/>
                            <DataGridTemplateColumn>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <ListView ItemsSource="{Binding ConnectionDates}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>