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

Problem with initially hidden columns in GridView

8 Answers 248 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.
Philip
Top achievements
Rank 1
Philip asked on 03 Jun 2009, 04:30 PM
Hello,

I am looking for some help with an issue concerning columns that are created with the IsVisible property set to false, then turned having their IsVisible properties set to true via a context menu. The initial code was created using code from the Telerik demo "Gridview/HeaderContextMenu" as a basis. The aim is to have columns that can be turned on or off with a menu, with some defaulted to on and some off. The ones defaulted to on work with no problem, the problem, however, is the ones defaulted to off do not appear when their IsVisible properties are set to true. However, they will finally appear if I switch tabs but even then the colum headers are missing.

I would appreciate any help I may recieve in understanding this problem and finding a solution. Below is the code for my context menu, and I can provide more if needed. To me it seems the overall way in which the gridview was implemented was very similiar to how the Telerik demo was, with the exception that the code begins with columns being set to hidden. That said, perhaps I am missing some important subtleties in what needs to be done to make this work correctly.

Thank everyone for their time,

Philip Coffey


    public class GridViewContextMenu
    {
        public GridViewContextMenu(GridView gridView)
        {
            this.GridView = gridView;
        }

        private GridView GridView { get; set; }


        public static readonly DependencyProperty IsEnabledProperty
            = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(GridViewContextMenu),
                new Telerik.Windows.PropertyMetadata(new PropertyChangedCallback(OnIsEnabledPropertyChanged)));

        public static void SetIsEnabled(DependencyObject dependencyObject, bool enabled)
        {
            dependencyObject.SetValue(IsEnabledProperty, enabled);
        }

        private static void OnIsEnabledPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            GridView gridView = dependencyObject as GridView;
            if (gridView != null)
            {
                if ((bool)e.NewValue)
                {
                    if (gridView.IsFilteringAllowed)
                    {
                        GridViewContextMenu menu = new GridViewContextMenu(gridView);
                        menu.Initialize();
                    }
                }
            }
        }

        private void Initialize()
        {
            RadContextMenu contextMenu = new RadContextMenu();
            StyleManager.SetTheme(contextMenu, StyleManager.GetTheme(this.GridView));

            RadMenuItem item = new RadMenuItem() { Header = "Add/Remove Columns" };
            contextMenu.Items.Add(item);

            foreach (GridViewColumn column in this.GridView.Columns)
            {
                RadMenuItem subMenu = new RadMenuItem()
                    {
                        Header = column.HeaderText,
                        IsCheckable = true,
                        IsChecked = true,
                        Tag = column
                    };


                subMenu.SetBinding(RadMenuItem.IsCheckedProperty, new Binding("IsVisible") { Mode = BindingMode.TwoWay, Source = column });

                subMenu.Click += new RoutedEventHandler(subMenu_Click);
                item.Items.Add(subMenu);
               
            }

            RadContextMenu.SetContextMenu(this.GridView, contextMenu);
        }
    }

8 Answers, 1 is accepted

Sort by
0
Accepted
Vlad
Telerik team
answered on 04 Jun 2009, 07:01 AM
Hello Philip,

I've made for you small example to illustrate you how to handle this scenario - you can find the project attached.

Let me know how it goes.

All the best,
Vlad
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
Philip
Top achievements
Rank 1
answered on 04 Jun 2009, 03:55 PM
Hello,

Thank you for the prompt reply. I have been experimenting with the code you offered, but have still had no luck getting the columns to show up if they have been initially set to invisible.

Here is the xaml and cs file I have made in an attempt to get what I am looking for. I am beginning to think it may have to do with something happening at the upper level of the UI, but maybe someone will see something I'm missing. I've left out some of the code in the c# file, but I don't believe it is relevent. If needed I can provide that as well.

Thanks again for the help,

xaml:
<local:StackStudioPageBase x:Class="ESC.SV.UI.Silverlight.StackStudio.Pages.PlantsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ESC.SV.UI.Silverlight.StackStudio.Pages"
    xmlns:silverlight="clr-namespace:ESC.SV.UI.Silverlight;assembly=ESC.SV.UI.Silverlight"
    xmlns:silverlightControls="clr-namespace:ESC.SV.UI.Silverlight.Controls;assembly=ESC.SV.UI.Silverlight"
    xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls.GridView;assembly=Telerik.Windows.Controls.GridView"
    xmlns:telerikControls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
    xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
    xmlns:telerikInputControls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input">
    <Grid x:Name="LayoutRoot" Background="White">

        <telerikGrid:RadGridView x:Name="GridView"
                                 AutoGenerateColumns="False"
                                 ScrollMode="RealTime"
                                 IsReadOnly="True"
                                 ShowGroupPanel="False"
                                 CanUserReorderColumns="False"
                                 CanUserFreezeColumns="False">
            <telerikGrid:RadGridView.Columns>
                <telerikGrid:GridViewDataColumn HeaderText="ID" DataMemberBinding="{Binding ID}"  IsVisible="True"/>
                <telerikGrid:GridViewDataColumn HeaderText="Name" DataMemberBinding="{Binding Name}"  IsVisible="False"/>
            </telerikGrid:RadGridView.Columns>

        </telerikGrid:RadGridView>

    </Grid>
    </local:StackStudioPageBase>

C# file (some code excluded):

{
            InitializeComponent();

            this.GridView.FrozenColumnCount = 1; // freeze the first column (Identifier) - can we make this permanent???

            GridViewContextMenu.SetIsEnabled(this.GridView, true); //set attached property to enable the grid's context menu...

            RadContextMenu contextMenu = new RadContextMenu();
            StyleManager.SetTheme(contextMenu, StyleManager.GetTheme(this.GridView));

            RadMenuItem item = new RadMenuItem() { Header = "Add/Remove Columns" };
            contextMenu.Items.Add(item);

            foreach (GridViewColumn column in this.GridView.Columns)
            {
                RadMenuItem subMenu = new RadMenuItem()
                {
                    Header = column.HeaderText,
                    IsCheckable = true,
                    //IsChecked = true
                };


                subMenu.SetBinding(RadMenuItem.IsCheckedProperty, new Binding("IsVisible") { Mode = BindingMode.TwoWay, Source = column });

                //contextMenu.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));
                item.Items.Add(subMenu);
            }

            RadContextMenu.SetContextMenu(this.GridView, contextMenu);
        }



0
Philip
Top achievements
Rank 1
answered on 12 Jun 2009, 12:58 PM
Problem has been resolved. The issue was the version of Telerik controls we had. Everything works as expected now.

Thank you for your help.
0
Devika
Top achievements
Rank 1
answered on 19 Jun 2009, 07:11 AM
Hi Philip,

Even i am facing the same problem. I am using the trial version of Q1 2009. Please let me know the version which you are using.

Thanks in advance
DV

0
Rossen Hristov
Telerik team
answered on 19 Jun 2009, 07:35 AM
Hello Devika,

You can try the latest official release which is 2009 Q1 SP2 (v 2009.1 526).

If you want the latest possible version, you can download one of our internal builds. They are provided each Friday. You can download the latest internal build from your account.

All the best,
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
Philip
Top achievements
Rank 1
answered on 19 Jun 2009, 12:14 PM
Hello Devika,

The version we had a problem was indeed the version you have. The one that worked for us was the one that Ross mentioned: 2009 Q1 SP2 (v 2009.1 526).

Hope this helps.

Philip
0
Devika
Top achievements
Rank 1
answered on 19 Jun 2009, 04:02 PM
Thanks a lot.. It worked with the version you mentioned.
Please let me know where i can get all the internal builds.

Regards
DV
0
Milan
Telerik team
answered on 22 Jun 2009, 06:18 AM
Hi Devika,

You can get the latest internal build from our download page. For every suite there you will find a link called "Latest Internal Builds". Just follow the link and you will be able to download our latest builds.

Regards,
Milan
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
Philip
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Philip
Top achievements
Rank 1
Devika
Top achievements
Rank 1
Rossen Hristov
Telerik team
Milan
Telerik team
Share this question
or