This is a migrated thread and some comments may be shown as answers.

[Solved] Parent-Child SelectionChanged Event

3 Answers 219 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
glen
Top achievements
Rank 2
glen asked on 06 May 2009, 08:46 AM
Dear Telerik,

I have a GridView with a Hierarchy setup, similar to your First Look example, however the gridview SelectionChanged event fires whether the parent or child event is changed. 

How would you recommend working out which one has actually changed and getting the relevant data back from the row selected?

Cheers

Glen

3 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 06 May 2009, 02:22 PM
Hello glen,

Along with attaching to the SelectionChanged event of the parent grid, you will need to attach to the SelectionChanged event of the RadGridView inside the HierarchyChildTemplate in order to receive notifications about the child grids.

Here is a small XAML snippet that demonstrates that:

<UserControl x:Class="Hierarchy.Page" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    Width="400" Height="300" 
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" 
             > 
    <Grid x:Name="LayoutRoot" Background="White"> 
        <telerik:RadGridView  
            x:Name="RadGridView1"  
            AutoGenerateColumns="False"  
            SelectionChanged="ParentGrid_SelectionChanged"> 
            <telerik:RadGridView.Columns> 
                <telerik:GridViewDataColumn HeaderText="Department" DataMemberBinding="{Binding DepartmentName}" /> 
                <telerik:GridViewDataColumn HeaderText="Staff count" DataMemberBinding="{Binding StaffCount}" /> 
            </telerik:RadGridView.Columns> 
 
            <telerik:RadGridView.HierarchyChildTemplate> 
                <DataTemplate> 
                    <StackPanel DataContext="{x:Null}"> 
                        <telerik:RadGridView  
                            Width="200"  
                            Loaded="RadGridView_Loaded"  
                            SelectionChanged="ChildGrid_SelectionChanged" 
                            ItemsSource="{Binding Staff}"   
                            ShowGroupPanel="False"  
                            Margin="20"  
                            IsFilteringAllowed="False"  
                            AutoGenerateColumns="False"> 
                            <telerik:RadGridView.Columns> 
                                <telerik:GridViewDataColumn HeaderText="First Name" DataMemberBinding="{Binding FirstName}" /> 
                                <telerik:GridViewDataColumn HeaderText="Last Name" DataMemberBinding="{Binding LastName}" /> 
                            </telerik:RadGridView.Columns> 
                        </telerik:RadGridView> 
                    </StackPanel> 
                </DataTemplate> 
            </telerik:RadGridView.HierarchyChildTemplate> 
        </telerik:RadGridView> 
    </Grid> 
</UserControl> 
 
     

In the event handler, you can obtain information about the data by casting the AddedRecords property of the SelectionChangedEventArgs to DataRecord:

        void RadGridView1_SelectionChanged(object sender, SelectionChangeEventArgs e) 
        { 
            if (e.AddedRecords.Count > 0) 
            { 
                DataRecord record = e.AddedRecords[0] as DataRecord; 
                var dataRecord = record as DataRecord; 
                if (dataRecord != null) 
                { 
                    MessageBox.Show(dataRecord.Data.ToString()); 
                } 
            } 
        } 
 


Please, let me know if this does not help or you need another functionality.

Sincerely yours,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
glen
Top achievements
Rank 2
answered on 06 May 2009, 02:31 PM
Hi Ross,

I create the Child data like your First Look demo :

void Example_Loaded(object sender, RoutedEventArgs e)

            TableRelation relation = new TableRelation();
            relation.FieldNames.Add(new FieldDescriptorNamePair("EmployeeID", "EmployeeID"));

            GridViewTableDefinition definition = new GridViewTableDefinition();
            definition.AutoGenerateFieldDescriptors = false;

etc etc

so I am trying to work out where, in this code-behind, I attach the SelectionChanged event ??

Glen


0
Rossen Hristov
Telerik team
answered on 06 May 2009, 03:21 PM
Hi glen,

In the demo you are talking about, if you check the XAML file you will see that there is a HierarchyChildTemplate. It is much more complex than the one I provided, but the idea is the same. There is nothing wrong with attaching the event handler in the XAML file. In this way it will be attached to every child grid that is created.

Anyway, it seems very reasonable that attaching a SelectionChanged event handler to the parent grid should also provide notification when the selection is changed in one of its child grids, so we will invesitgate and fix this.

Until then our suggestion is to manually attach the event handler to the grid in the HierarchyChildTemplate in the XAML file.

Please, excuse us for the inconvenience.

Sincerely yours,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
glen
Top achievements
Rank 2
Answers by
Rossen Hristov
Telerik team
glen
Top achievements
Rank 2
Share this question
or