7

I have a DataGrid (from the toolkit) and I want to nest another DataGrid in the DataGrid.RowDetailsTemplate. The trick is I want to bring back the data from one table in the main grid and then based on row selection go and get additonal detail from a different table and show it in the DataGrid in the detail template.

This is easy enough to do in 2 seperate DataGrids but I am having trouble getting it to work with the nested version.

Is this even possible? If so, could someone point me in the right direction. I should note I am using LinqToSql clases to populate the data.

Thanks for your consideration. -Joel

jvberg
  • 273
  • 3
  • 9
  • 2
    In the name of all that is good and holy, please please _please_ do not nest a `datagrid` in another `datagrid`. – Greg D May 02 '10 at 13:06
  • @GregD can you please explain why you wouldn't do something like that? – SoftwareSavant Jun 13 '13 at 14:23
  • With the exception of a raw DBMS, a DataGrid is practically always the wrong user experience. It's heavy, it's complex, it's dense, it includes no effective contextual information, and it's indicative of a failure to design a good ux. Layering the heavy, complex control with _another_ heavy, complex control is just a nightmare scenario. If you have hierarchical data, consider copying the Windows Explorer paradigm. The lighter-weight listview combined with a tree control is _very_ well-known and well-understood paradigm. Do _not_ nest a datagrid in another datagrid. – Greg D Jun 14 '13 at 13:39

1 Answers1

5

If you are using LinqToSQL you can easily do this using an association. In my practice I have created two tables:

GuyTable

  • First Name
  • Last Name
  • UniqueID

GuyActionsTable

  • UniqueID
  • GuyID
  • Action Description

I created a one-to-many relationship from GuyTable.UniqueID to GuyActionsTable.GuyID called "GuyActions"

I then bind my DataGrid like this. Excuse any errors as I am doing this by hand:

<w:DataGrid ItemsSource={Binding Source={StaticResource YourDataSource}}>
<w:DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <w:DataGrid ItemsSource={Binding GuyActions}>
            <w:DataGrid.Columns>
                <w:DataGridTextColumn Header="Action" DisplayMemberBinding="{Binding Action_Description}" />
            </w:DataGrid.Columns>
        </w:DataGrid>
    </DataTemplate>
</w:DataGrid.RowDetailsTemplate>
<w:DataGrid.Columns>
    <w:DataGridTextColumn Header="First Name" DisplayMemberBinding="{Binding First_Name}" />
    <w:DataGridTextColumn Header="Last Name" DisplayMemberBinding="{Binding Last_Name}" />
</w:DataGrid.Columns>

sidney.andrews
  • 5,146
  • 3
  • 23
  • 29