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

Hierarchy Binding

1 Answer 49 Views
GridView
This is a migrated thread and some comments may be shown as answers.
raghul
Top achievements
Rank 1
raghul asked on 06 Dec 2011, 07:28 AM
Hi,

I have got a hierarchical Gridview in which the parent grid displays the country details and the child displays all the states of that county. I have two doubts to be clarified.

1. How to get the data from the expanded row, when I use grid.selectedItem it returns null
2. How to bind the Items in the child grid in run time. Please find my coding which I have developed and let me know where I have done the mistake.

<telerik:RadGridView VerticalAlignment="Top" FontSize="12" IsReadOnly="True" 
         AutoGenerateColumns="False" ShowGroupPanel="False" IsFilteringAllowed="True" Name="grdTestCase"
                            ItemsSource="{Binding Country}" RowIndicatorVisibility="Collapsed"
                            RowIsExpandedChanged="grdCountry_RowExpanded">
           <telerik:RadGridView.ChildTableDefinitions>
               <telerik:GridViewTableDefinition />
           </telerik:RadGridView.ChildTableDefinitions>
           <telerik:RadGridView.Columns>
               <telerik:GridViewDataColumn Header="Country Id" DataMemberBinding="{Binding CountryId}" UniqueName="CountryId" Width="150" />
               <telerik:GridViewDataColumn Header="Country Name" DataMemberBinding="{Binding CountryName}" UniqueName="CountryName" Width="*"/>
           </telerik:RadGridView.Columns>
           <telerik:RadGridView.HierarchyChildTemplate>
               <DataTemplate>
                   <StackPanel>
                       <telerik:RadGridView ItemsSource="{Binding lstState}"  ShowGroupPanel="False" FontSize="11"
                                            IsFilteringAllowed="False" AutoGenerateColumns="False"
                                            ShowInsertRow="False"  RowIndicatorVisibility="Collapsed">
                               <telerik:RadGridView.Columns>
                               <telerik:GridViewDataColumn Header="State Id" DataMemberBinding="{Binding StateId}" UniqueName="CountryId" Width="150" />
                               <telerik:GridViewDataColumn Header="State Name" DataMemberBinding="{Binding StateName}" UniqueName="CountryName" Width="*"/>
                               </telerik:RadGridView.Columns>
                           </telerik:RadGridView>
                   </StackPanel>
               </DataTemplate>
               </telerik:RadGridView.HierarchyChildTemplate>
       </telerik:RadGridView>

public void grdCountry_RowExpanded(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            if ((e.Row as GridViewRow).IsExpanded)
            {
                IEnumerable<State> IemState = from C in lstState
                                              where C.CountryId == 1
                                              select C;
 
                lstState = IemState.ToList();
            }
        }
        public IList<Country> GetCountries()
        {
            lstCountry.Add(new Country(91, "India", lstState));
            lstCountry.Add(new Country(1, "U S A", lstState));
            lstCountry.Add(new Country(61, "Australia", lstState));
            lstCountry.Add(new Country(51, "N Z", lstState));
            return lstCountry;
        }
 
        public IList<State> GetState()
        {
            lstState.Add(new State(1, "Andra Pradesh", 91));
            lstState.Add(new State(2, "Tamil Nadu", 91));
            lstState.Add(new State(3, "Kerala", 91));
            lstState.Add(new State(4, "Alabama", 1));
            lstState.Add(new State(5, "Arizona", 1));
            lstState.Add(new State(6, "ALASKA", 1));
            lstState.Add(new State(7, "New South Wales", 61));
            lstState.Add(new State(8, "Northern Territory", 61));
            lstState.Add(new State(9, "Queensland", 61));
            lstState.Add(new State(10, "South Australia", 61));
            lstState.Add(new State(11, "ANZUS", 51));
            return lstState;
        }

 

1 Answer, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 06 Dec 2011, 07:46 AM
Hi,

 I strongly suggest you to check our demos in "Hierarchy" category.

Regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
raghul
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Share this question
or