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

Hide/Show GridViewDataColumn with databinding

4 Answers 806 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.
LE DREAU Steeve
Top achievements
Rank 1
LE DREAU Steeve asked on 08 Feb 2010, 09:57 AM
Hi,

I'm trying to bind a boolean value to the IsVisible property of a GridViewDataColumn to show/hide a column through MVVM pattern.
I have a viewmodel with a "ShowNameColumn" boolean property. When I set this property, I raise a onpropertychanged event to tell the view to refresh the property value.
This property is bound to IsVisible property of my gridviewcolumn like this :
<telerikGridView:GridViewDataColumn Width="5*" IsVisible={Binding ShowNameColumn} DataMemberBinding="{Binding Name}" Header="Name" /> 

But I have an exception when running this :
AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 27 Position: 74]

So my first question is :
When you uyse databindings on gridviewDataColumn, DataMemberBinding will try to get the value from the current bound item that gridview is working on, but is I use binding on other properties like IsVisible, does your gridview get the value from the datacontext of the view or from the item the gridview is parsing ?

My second question :
How can I Show/Hide a column through binding ?

Thanks,

Regards,
Steeve

4 Answers, 1 is accepted

Sort by
0
Accepted
Vlad
Telerik team
answered on 08 Feb 2010, 10:07 AM
Hello Steeve,

You cannot bind column properties since Binding in Silverlight 3 is supported only for FrameworkElement – grid columns are plain DependencyObjects.

Sincerely yours,
Vlad
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
LE DREAU Steeve
Top achievements
Rank 1
answered on 08 Feb 2010, 10:59 AM
Thanks for your quick answer Vlad.
Bye
0
Dimitrina P
Top achievements
Rank 1
answered on 11 Apr 2011, 02:29 PM
Now, with Silverlight 4 it is possible to show/hide GridViewDataColumn with databinding. 
You should define your property in your view model
public class EmployeeHolder : INotifyPropertyChanged
    {
        bool _isVisible = false;
        public bool IsVisible
        {
            get { return _isVisible; }
            set
            {
                _isVisible = value;
                RaisePropertyChanged("IsVisible");
            }
        }
 
        public List<Employee> Employees
        {
            get
            {
                List<Employee> employees = new List<Employee>();
                employees.Add(new Employee() { EmployeeFirstName = "Name1" });
                employees.Add(new Employee() { EmployeeFirstName = "Name2" });
                return employees;
            }
        }
}
public class Employee
{
    public string EmployeeFirstName { get; set; }
}

After that you need to bind the IsVisible property of the column:
<CheckBox IsChecked="{Binding IsVisible, Mode=TwoWay}" Content="Is Visible" Margin="3"/>
<telerik:RadGridView ItemsSource="{Binding Employees}" AutoGenerateColumns="False">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn UniqueName="FirstName" Header="First Name" DataMemberBinding="{Binding EmployeeFirstName}"
                                    IsVisible="{Binding IsVisible, Source={StaticResource EmployeeHolder}}"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

Kind Regards,
Didie
0
Brian
Top achievements
Rank 1
answered on 17 May 2011, 07:48 PM
In your example, you are binding whether to show a column based on a property for the entire row.  What if I want to hide 2 columns but show two others?  This method won't work in that case. 

I'm trying to bind the IsVisible property of a GridViewDataColumn to a property in the supporting view model.  The view model contains the ItemsSource for the RadGridView.  This is not working and I get no binding error of any kind.  Should this work?

<telerik:GridViewDataColumn DataMemberBinding="{Binding LastName}" IsVisible="{Binding ElementName=dgTestData, Path=DataContext.ShowLastNameColumn, Mode=TwoWay}">
Tags
GridView
Asked by
LE DREAU Steeve
Top achievements
Rank 1
Answers by
Vlad
Telerik team
LE DREAU Steeve
Top achievements
Rank 1
Dimitrina P
Top achievements
Rank 1
Brian
Top achievements
Rank 1
Share this question
or