1

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.

3
  • It is possible that there is a problem in my design in my example. In fact it is not the real design that I am using. It is a fake example because I am working on a confidential project. Sorry about that. I should have think more about it :/ Commented Oct 24, 2013 at 19:16
  • Feel free to edit your question in order to make it clearer (and i'll edit my answer accordingly off course). Commented Oct 24, 2013 at 20:00
  • @MyRestlessDream there's no reason to include the solution in your question, that's why you mark an answer as accepted. Commented Mar 1, 2014 at 12:08

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

This assumes that Employee has a list of ComputerIDs, if on the other hand, Connections is the list, then each Connection would have only one ComputerID, or it wouldn't look like the image you posted :P... In this case the binding would be ItemsSource="{Binding Path="Connection.ComputerID"}". Also, this assumes you use the getters of LastComputerUsed and LastConnectionDate to obtain the appropriate values. Feel free to post a comment if you have any additional questions (it doesn't look exactly like your image, after all, even though it could ^_^)...
ItemsSource="{Binding Path="Connections"}", <DataGridTextColumn Header="Computer ID" Binding="{Binding ComputerID.ID}"/>, ` <ListView ItemsSource="{Binding ComputerID.ConnectionDates}"/>`, sorry...
Thank you a lot ! I have almost succeed to what I would like to do :D But I have another question. Do you know if it is possible to add a Grid/other layout instead of the new DataGrid ? I would like to organize the data as I want. Do you think it is possible ?
You can add whatever you like as long as it has ItemsSource (i.e. a way to bind to a collection and get a representation of each item) - anything that derives from ItemsControl, and supports a custom DataTemplate for columns for the third list nesting (e.g. GridViewColumn.CellTemplate for GridView/ListView), though i believe styling it (link :P) appropriately will be more important than the choice of control itself.
Oki thank you a lot Stefan! I really appreciate your help/advices/links. I will try some more stuff and I will let you know :)
0

Lets try,

You can create a class named EmployeeConnection that will have

public Employee Employee { get; private set; } public List Connections { get; private set; }

So, every row in your gridview will have 1 Employee and columns with Connection.

You have to make EmployeeConnection as your gridview Data Source.

could be?

1 Comment

Thank you for your answer. That was not exactly what I would like to do because it would be great to not add a new class just for that :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.