How to access DataGrid in RowDetail by name in WPF ?

Mojtaba_Hakim 281 Reputation points
2022-06-24T09:16:09.68+00:00

I'm using C# WPF ,

I want to access item's of datagrid in row detail of the main datagrid , but I can't :

214745-2.png

Please help me

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,238 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. krishna 466 Reputation points
    2022-08-31T04:03:20.607+00:00

    gvPrintDetail is the nested grid in XAML ,Main grid should have LoadingRowDetails event on click of the expander ,loading row details event is fired

      public DataGrid gvPrintDetail = new DataGrid();  
          
        private void MainGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)  
                {  
                    try  
                    {  
                          
                        DataRowView row = MainGrid.SelectedItem as DataRowView;  
                        lsStageId = row["STAGE"].ToString();  
                        lsDocNum = row["DOC_NO"].ToString();  
                        lsOrgDup = row["ORG_DUP"].ToString();  
                        gvPrintDetail = (DataGrid)e.DetailsElement.FindName("gvPrintDetail");  
                         
                    }              
                     catch (Exception ex)  
                    {  
                        MessageBox.Show(ex.Message);  
                    }  
          
                }  
    
    1 person found this answer helpful.
    0 comments No comments

  2. Hui Liu-MSFT 38,251 Reputation points Microsoft Vendor
    2022-06-27T02:06:01.53+00:00

    For accessing the controls in the DataTemplate, you could try the code below.
    Xaml:

     <Window.DataContext>  
            <local:ViewModel/>  
        </Window.DataContext>  
        <StackPanel>  
            <DataGrid Name="dg1" AutoGenerateColumns="False" SelectionMode="Single" CanUserAddRows="false"   
                      CanUserDeleteRows="False" SelectionUnit="FullRow" ItemsSource="{Binding View}" >  
                <DataGrid.CellStyle>  
                    <Style TargetType="DataGridCell">  
                        <Setter Property="BorderThickness" Value="0"/>  
                    </Style>  
                </DataGrid.CellStyle>  
                <DataGrid.RowHeaderTemplate>  
                    <DataTemplate>  
                        <Expander Expanded="Expander_Expanded" Collapsed="Expander_Collapsed">  
                        </Expander>  
                    </DataTemplate>  
                </DataGrid.RowHeaderTemplate>  
                <DataGrid.Columns>  
                    <DataGridTextColumn Header="ID" IsReadOnly="True" Width="100" Binding="{Binding ID}" />  
                    <DataGridTextColumn Header="Name" IsReadOnly="True" Width="100" Binding="{Binding Name}" />  
                </DataGrid.Columns>  
                <DataGrid.RowDetailsTemplate>  
                    <DataTemplate>  
                        <DataGrid x:Name="dg" ItemsSource="{Binding Details}" Initialized="dg_Initialized"  >  
                            <DataGrid.Columns>  
                                <DataGridTextColumn  IsReadOnly="True" Width="100" Binding="{Binding Title}" />  
                                <DataGridTextColumn Header="Address" IsReadOnly="True" Width="100" Binding="{Binding Address}" />  
                            </DataGrid.Columns>  
                        </DataGrid>  
                    </DataTemplate>  
                </DataGrid.RowDetailsTemplate>  
            </DataGrid>  
    
        </StackPanel>  
    

    Codebehind:

     public partial class MainWindow : Window  
      {  
    
        public MainWindow()  
        {  
          InitializeComponent();  
    
        }  
        private void Expander_Expanded(object sender, RoutedEventArgs e)  
        {  
          for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)  
            if (vis is DataGridRow)  
            {  
              var row = (DataGridRow)vis;  
              row.DetailsVisibility = row.DetailsVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;  
              break;  
            }  
    
    
        }  
    
        private void Expander_Collapsed(object sender, RoutedEventArgs e)  
        {  
          for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)  
            if (vis is DataGridRow)  
            {  
              var row = (DataGridRow)vis;  
              row.DetailsVisibility = row.DetailsVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;  
              break;  
            }  
        }  
        public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject  
        {  
          if (depObj != null)  
          {  
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)  
            {  
              DependencyObject child = VisualTreeHelper.GetChild(depObj, i);  
    
              if (child != null && child is T)  
                yield return (T)child;  
    
              foreach (T childOfChild in FindVisualChildren<T>(child))  
                yield return childOfChild;  
            }  
          }  
        }  
    
        private void dg_Initialized(object sender, System.EventArgs e)  
        {  
          foreach (var datagrid in FindVisualChildren<DataGrid>(this))  
          {  
            if (datagrid.Name == "dg")  
            {  
    
              datagrid.Columns[0].Header = "MyTitle";  
              datagrid.AutoGenerateColumns=false;  
              datagrid.CanUserAddRows = true;  
              datagrid. CanUserDeleteRows =true;  
            }  
          }  
        }  
      }  
    

    The result:
    215118-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments