Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
2.11/5 (2 votes)
See more:
Hello,
I am designing a wpf application, now my requirement is that I want to place a parent child grid. A parent record should posses a expand button so that the child reords will be present in it.... Please I am new to this Parent-child Grid.... Can any one help out me by providing the code....It is also called as Nested Grid
Posted

XML
<DataGrid AutoGenerateColumns="False" MaxHeight="525" Height="auto" RowBackground="White"  HeadersVisibility="Column"  HorizontalAlignment="Left"  Background="#E7F1FA" AlternatingRowBackground="LightGray" Name="grdKitsGroupingParent" VerticalAlignment="Top"  CanUserAddRows="False" Width="1130" Margin="5,194,0,0" Grid.Column="1"  RowDetailsVisibilityMode="VisibleWhenSelected" RowDetailsVisibilityChanged="grdKitsGroupingParent_RowDetailsVisibilityChanged">
                      <DataGrid.Columns>
                          <DataGridTemplateColumn Header="C" >
                              <DataGridTemplateColumn.CellTemplate>
                                  <DataTemplate>
                                      <StackPanel Name="stk">
                                          <Button x:Name="btnCollapse"  ToolTip="Expand" Foreground="Black" Background="Transparent" BorderBrush="Transparent" Tag="{Binding KitGroupingID}" Click="btnCollapse_Click"    >
                                              <Image Source="../images/Plus.png" Cursor="Hand"  Height="22" Width="18"  Name="imgExpand" ></Image>
                                          </Button>
                                      </StackPanel>
                                  </DataTemplate>
                              </DataGridTemplateColumn.CellTemplate>
                          </DataGridTemplateColumn>
                          <DataGridTemplateColumn Header="D">
                              <DataGridTemplateColumn.CellTemplate>
                                  <DataTemplate>
                                      <StackPanel>
                                          <Button x:Name="DeleteButton" ToolTip="Delete" Background="Transparent" BorderBrush="Transparent"    Foreground="Black" Tag="{Binding KitGroupingID}" Click="btnDelete_Click"   >
                                              <Image Source="../images/Deleteicon.png" Cursor="Hand" Height="22" Width="18"  ></Image>
                                          </Button>
                                      </StackPanel>
                                  </DataTemplate>
                              </DataGridTemplateColumn.CellTemplate>
                          </DataGridTemplateColumn>
                          <DataGridTemplateColumn Header="S"  >
                              <DataGridTemplateColumn.CellTemplate>
                                  <DataTemplate>
                                      <StackPanel>
                                          <Button x:Name="btnSaveParent"  ToolTip="Save" Foreground="Black" Background="Transparent"  BorderBrush="Transparent"  Tag="{Binding KitGroupingID}" Click="btnSaveParent_Click"  >
                                              <Image Source="../images/Save.png" Cursor="Hand" Height="22" Width="18"   ></Image>
                                          </Button>
                                      </StackPanel>
                                  </DataTemplate>
                              </DataGridTemplateColumn.CellTemplate>
                          </DataGridTemplateColumn>


                          <DataGridTextColumn CanUserReorder="True"  CanUserResize="True" CanUserSort="True" Width="200" Binding="{Binding KitGroupName}" Header="Group Name"  Foreground="Black">
                          </DataGridTextColumn>

                          <DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="190*" Binding="{Binding Inventory}" Header="Inventory"  Foreground="Black">
                          </DataGridTextColumn>

                      </DataGrid.Columns>
                      <DataGrid.RowDetailsTemplate>
                          <DataTemplate>
                              <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"   >
                                  <DataGrid  Name="NestedDG"   CanUserAddRows="False"  Background=" White" AlternatingRowBackground="LightGray" Width="1060"  HeadersVisibility="Column" AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected" RowDetailsVisibilityChanged="ParentDG_RowDetailsVisibilityChanged"  >
                                      <DataGrid.Columns>

                                          <DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="700*" Binding="{Binding LineItem}" Header="Line Item"  Foreground="Black">
                                          </DataGridTextColumn>

                                      </DataGrid.Columns>
                                   
                  </DataGrid>
 
Share this answer
 
v2
If you asking for DataGrid, I don't know too much about it.

Do you meant when user select a parent record in one ListView(A), all child records is shown in another ListView(B)?
If so, you have to create a ViewModel that represent the parents, which have ChildRecords properties that link to child records, and let (B) bind to (A)'s ChildRecords.

Or you want to show in the same ListView?
If so, in addition to the viewmodel, you have to change ListView's ItemContainerStyle:
XML
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  
  <Grid>  
    
    <ListView>
      <ListView.ItemContainerStyle>
      
        <Style TargetType="ListBoxItem">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="ListBoxItem">
                <StackPanel>
                  <ContentPresenter />
                  <ListBox Name="Childs" Visibility="Collapsed">
                    <TextBlock Text="Child1" />
                    <TextBlock Text="Child2" />
                    <TextBlock Text="Child3" />
                  </ListBox>
                 </StackPanel>
                <ControlTemplate.Triggers>
                  <Trigger Property="IsSelected" Value="True">
                    <Setter TargetName="Childs" Property="Visibility" Value="Visible" />
                  </Trigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      
      </ListView.ItemContainerStyle>        
      <TextBlock Text="1" />
      <TextBlock Text="2" />
      <TextBlock Text="3" />
    </ListView>
  </Grid>
</Page>


Regards
Joseph Leung
 
Share this answer
 
v2
Comments
jing567 6-Nov-13 23:14pm    
hei,
I am sorry , I am asking for Datagrid yar...
Leung Yat Chun 7-Nov-13 2:54am    
no problem, other members may have a better solution.

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