in my application I have a RadGridView for who I create his columns dynamically on a specific event in my application. For each row in this grid, I need a sub grid. To achieve this, I create a GridViewToggleRowDetailsColumn dynamically.
In my XAML, I defined a <telerik:RadGridView.RowDetailsTemplate><DataTemplate><telerik:RadGridView x:Name="subSetGrid".
If I want to create dynamically the columns of my "subSetGrid" and assign it's "DataSource" to simething, how I can do to achieve this???
5 Answers, 1 is accepted
The RowDetails are a part of the corresponding row, so you can easily get all the elements using ChildrenOfType<T> () extension method. For example if your main GridView is 'clubsGrid':
In the case above 'child' will be the first RadGridView defined inside the template.
the Telerik team
if I create columns dynamically at my app startup just before assigning ItemSource to my grid, can you tell me if the proposed solution should work too?
Actually the last suggestion that I gave you is not a good one. It is much more better to use the events of the parent or the details GridView.
For example you could use the RowDetailsVisibilityChanged event of the parent RadGridView. Then when the row is expanded you could add the desired columns and set the ItemsSource of the e.DetailsElement (that is the subSetGrid in your case). Or you could subscribe for the Loaded event of the Details RadGridView (subSetGrid) and then add the desired columns and set the ItemsSource there.
I am not sure what do you mean by "create columns dynamically at my app startup just before assigning ItemSource to my grid". Do you mean that you would like to initially create the columns and then set the ItemsSource?
the Telerik team
what I mean by "create columns dynamically at my app startup just before assigning ItemSource to my grid" is when my app start, I call my backend to get all grids definitions, col type, alignment, etc... When I get all the definitions, I create dynamically the columns in each grids. After that, I set the datasource of each grid regarding the data I need to have base of my columns definition.
In my DataTemplate for my rowdetail. I have more than one control, I have a page control with 4 pages, in each page I have a grid.
So the problem is If I want to create dynamically the columns of each subgrids at my app startup I don't know how todo it. Regarding of the columns I created, I have to create alson a dynamic ItemSource regarding the data my cutomer need to have in their grids.
Here is what you should do.
1. Attach to the LoadingRowDetais event of the master grid. This event is fired only once for a given row -- the very first time that the end user expands its row details.
2. In the event handler of the event, you have e.DetailsElement. This is the top-level FrameworkElement that you have placed in your RowDetailsTemplate. If this is the child grid you can cast it directly to RadGridView. If this top-level element is something else, let's say a StackPanel or another container that contains the child grid, you should find the reference to your child grid by calling the ChildrenOfType<RadGridView> method on e.DetailsElement.
3. Once you have a reference to the child grid, you can do anything in the world to it, including adding columns based on your criteria and setting its ItemsSource. You have a reference to the grid so you are free to do anything with it.
Of course -- these procedure will the same each time the user open's a row details for the first time. You can't get hold of a child grid before that because it has not been created yet. RowDetails are loaded on demand, which means that if the end user never expands the row details for a certain row, then they will never be loaded. This is for performance reasons.
I hope this makes sense.
Please, read the row details documentation carefully. All of the above is already described there. This topic is the one that exactly describes what you are looking for.
Thank you for you understanding.
the Telerik team