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

Programmatically set child grid ItemsSource, DataContext and columns

3 Answers 527 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.
Chris
Top achievements
Rank 1
Chris asked on 18 Aug 2011, 06:46 PM

I need to programmatically set the DataContext, ItemsSource, and columns of a child grid within a HierarchyChildTemplate. 

I have a situation where users need to select the object type and columns that will displayed in the child grid at runtime, therefore, the objects bound to the child grid are unknown at design time.  Additionally, the child objects not necessarily accessible through the object type bound to the parent grid and may require the child grid’s DataContext to be set separately of the parent grid’s context.

I have been trying a variety of approaches with no usable results.  Any input or suggestions are sincerely appreciated.

3 Answers, 1 is accepted

Sort by
1
Vanya Pavlova
Telerik team
answered on 18 Aug 2011, 07:15 PM
Hi Chris,

 
You may subscribe to the DataLoading event of RadGridView if you need to access child table definitions:

private void employeeRadGridView_DataLoading(object sender, GridViewDataLoadingEventArgs e)
{
    GridViewDataControl dataControl = (GridViewDataControl) sender;
    if (dataControl.ParentRow != null)
    {
           
           dataControl.ShowGroupPanel = false;
           GridViewDataColumn column = new GridViewDataColumn();
           column.DataMemberBinding = new Binding("EmployeeID");
           dataControl.Columns.Add(column);
           column = new GridViewDataColumn();
           column.DataMemberBinding = new Binding("FirstName");
           dataControl.Columns.Add(column);
           column = new GridViewDataColumn();
           column.DataMemberBinding = new Binding("LastName");
           dataControl.Columns.Add(column);
           column = new GridViewDataColumn();
           column.DataMemberBinding = new Binding("Title");
           dataControl.Columns.Add(column);
     }
}


Keep in mind that the DataContext of child RadGridViews is their parent data item. In case you have to assign a different DataContext you should create your own DataContextProxy to get it work.


Best wishes,
Vanya Pavlova
the Telerik team

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

0
Chris
Top achievements
Rank 1
answered on 18 Aug 2011, 08:01 PM
Thank you, Vanya.

Is it possible to access the Child Grids after the parent grid has loaded data?  In my scenario the user may request to show one or multiple additional object types in the child grid(s) or change the object type shown in the child grid(s).  Users may also add or remove columns in the individual child grids once the controls have completed loading.
0
Vanya Pavlova
Telerik team
answered on 22 Aug 2011, 07:51 AM
Hi Chris,

 
You may built dynamically your child table definitions through assigning a dynamical HierarchicalDataTemplate as shown below:

MainPage.xaml

<UserControl.Resources>
    <DataTemplate x:Key="dt">
        <telerik:RadGridView ItemsSource="{Binding Collection}" AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property1}"/>
                <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property2}"/>
                <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property3}"/>
                <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property3}"/>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </DataTemplate>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
        <telerik:RadGridView x:Name="parentGrid" AutoGenerateColumns="False" Margin="56,48,112,88" ItemsSource="{Binding Collection}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property1}"/>
                <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property2}"/>
                <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property3}"/>
                <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property3}"/>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        <telerik:RadButton Content="Generate Hierarchy" Click="RadButton_Click" HorizontalAlignment="Left" Height="40" Margin="136,0,0,24" VerticalAlignment="Bottom" Width="168"/>
    </Grid>
</UserControl>

MainPage.xaml.cs:

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();
        }
 
        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            GridViewTableDefinition dt = new GridViewTableDefinition();
            this.parentGrid.ChildTableDefinitions.Add(dt);
            this.parentGrid.HierarchyChildTemplate = this.Resources["dt"] as DataTemplate;
        }
    }


You may read more about different events of RadGridView in our online documentation, following this link.
Furthemore I mentioned that if you want to bind your child grids to a DataContext different than its parent item you have to create your own DataContextProxy. I believe that the following blog post might be of help.

Regards,
Vanya Pavlova
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Chris
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Chris
Top achievements
Rank 1
Share this question
or