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

Access grid columns in a DataTemplate from code behind

10 Answers 445 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joe Lozina
Top achievements
Rank 1
Joe Lozina asked on 23 Jun 2010, 12:30 PM
Hi,
I have a datagrid which has a Hierarchy Child Template and i want to show and hide GridViewDataColumns in the child grid RadGridViewChild based on a button that is outside the grid.

<telerik:RadGridView x:Name="RadGridViewParent" CanUserFreezeColumns="False" GridLinesVisibility="Horizontal" ItemsSource="{Binding Orders}"  IsReadOnly="True" AutoGenerateColumns="False"
.................... 
            <telerik:RadGridView.HierarchyChildTemplate> 
                <DataTemplate x:Name="dataTemplate"
                    <telerik:RadGridView x:Name="RadGridViewChild" CanUserFreezeColumns="False" AutoGenerateColumns="False" ItemsSource="{Binding Details}"  ShowGroupPanel="False" IsReadOnly="True"
                        <telerik:RadGridView.Columns> 
                            <telerik:GridViewDataColumn DataMemberBinding={Binding ProductID}" Header="Product ID" x:Name="ProductID" /> 
..................... 


So I would like to know how to access(show and hide) GridViewDataColumns in grid that is in a DataTemplate from a button click in the code behind.

Thanks,
Joe

10 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 23 Jun 2010, 12:40 PM
Hi Joe Lozina,

 Once you set the DataTemplate, you cannot modify it directly from code-behind. However, you can access all the expanded child grids like so:

var children = this.RadGridViewParent.ChildrenOfType<RadGridView>();

Once you have all the child grids, you can add and remove columns for each one programatically

All the best,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joe Lozina
Top achievements
Rank 1
answered on 24 Jun 2010, 01:58 AM
Hi Yavor,

I tried this and the count for Child Grids is 0 when I run the code.

I found out if I expand the Child grid then the method ChildrenOfType<RadGridView>() it returns the grid only of the grids that have been expanded.

So If I expand and show one child grid then hide columns it works correctly, then if I go to expand another child grid the 2 sets of child grids don't have the same amount of columns. So it looks like it creates the child grid once you click the expander.

How can I keep the child grids that are expanded/created in sync with those that haven't been expanded/created in sync?

Regards,
Joe




0
Yavor Georgiev
Telerik team
answered on 24 Jun 2010, 09:39 AM
Hi Joe Lozina,

 Please find attached a sample solution that tracks the expanded grids in a collection. Don't hesitate to get back to me in case you need further assistance.

Regards,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joe Lozina
Top achievements
Rank 1
answered on 25 Jun 2010, 12:45 AM
Hi Yavor,

When I try running the sample attached I get the following exception

System.ArgumentNullException was unhandled by user code Message=Value cannot be null.

on this line   - var rowIndex = this.clubsGrid.ItemContainerGenerator.IndexFromContainer(row);

the row variable from the previous line is null - var row = button.ParentOfType<GridViewRow>();

Regards,
Joe
0
Yavor Georgiev
Telerik team
answered on 25 Jun 2010, 09:42 AM
Hello Joe Lozina,

 Have you made any modifications to the project? Also, please try to remove references to Telerik assemblies and add them again (using the assemblies in the Libs folder).

Greetings,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joe Lozina
Top achievements
Rank 1
answered on 25 Jun 2010, 10:11 AM
Hi Yavor,

I've made no modifications to the project and I removed the references and readded them and I get that error.

I downloaded the sample again, removed and added the references, ran the project and got the same exception.

I'm using telerik 2010.1.603.1040 release.

Regards,
Joe

0
Yavor Georgiev
Telerik team
answered on 25 Jun 2010, 10:32 AM
Hello Joe Lozina,

 I have modified the sample to use another approach. Please let me know if it works for you now.

Greetings,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joe Lozina
Top achievements
Rank 1
answered on 28 Jun 2010, 01:07 AM
Hi Yavor,

It doesnt seem to work on this line this.childGrids.Add(rowIndex, row.FindChildByType<RadGridView>());.

The row.FindChildByType<RadGridView>() returns null. So it adds a null as the value to the Dictionary

Regards,
Joe
0
Yavor Georgiev
Telerik team
answered on 28 Jun 2010, 01:59 PM
Hello Joe Lozina,

 Probably on your end the IsExpandedChanged event fires before the row's hierarchy child loads. Here's a slight modification to the code-behind to account for that:

private Dictionary<int, RadGridView> childGrids;
        public MainPage()
        {
            InitializeComponent();
 
            this.childGrids = new Dictionary<int, RadGridView>();
            this.clubsGrid.DataLoading += new EventHandler<GridViewDataLoadingEventArgs>(clubsGrid_DataLoading);
            EventManager.RegisterClassHandler(this.GetType(), GridViewRow.IsExpandedChangedEvent, new RoutedEventHandler(GridViewRow_IsExpandedChanged), true);
        }
 
        void clubsGrid_DataLoading(object sender, GridViewDataLoadingEventArgs e)
        {
            var grid = sender as RadGridView;
            if (grid.ParentRow != null)
            {
                var rowIndex = this.clubsGrid.ItemContainerGenerator.IndexFromContainer(grid.ParentRow);
                this.childGrids.Add(rowIndex, grid);
            }
        }
 
        private void GridViewRow_IsExpandedChanged(object sender, RoutedEventArgs e)
        {
            var eventArgs = e as RadRoutedEventArgs;
            if (eventArgs != null)
            {
                var row = eventArgs.OriginalSource as GridViewRow;
                if (row != null)
                {
                    var rowIndex = this.clubsGrid.ItemContainerGenerator.IndexFromContainer(row);
                    if (!row.IsExpanded)
                    {
                        this.childGrids.Remove(rowIndex);
                    }
                }
            }
        }

I hope this helps. Please let me know how it goes.

Kind regards,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joe Lozina
Top achievements
Rank 1
answered on 30 Jun 2010, 02:31 AM
Hi Yavor,

That work, thanks.

Regards,
Joe
Tags
GridView
Asked by
Joe Lozina
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Joe Lozina
Top achievements
Rank 1
Share this question
or