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

Show/Hide Columns Question

3 Answers 164 Views
GridView
This is a migrated thread and some comments may be shown as answers.
toddhd
Top achievements
Rank 1
toddhd asked on 02 Mar 2011, 10:29 PM
I am trying to show/hide columns in the GridView by Binding checkboxes to it, just like this example:

http://www.telerik.com/help/silverlight/radgridview-how-to-show-hide-columns-outside-of-the-radgridview.html

However, it is failing in my project. Is there some setting I'm adding/missing that is breaking this? The following actually throws an error and the designer won't render...

        <StackPanel x:Name="CustomizeGrid" Background="Transparent" Orientation="Horizontal">
            <ListBox ItemsSource="{Binding Columns, ElementName=WorklistGridView}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Header}" IsChecked="{Binding IsVisible, Mode=TwoWay}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <telerik:RadGridView x:Name="WorklistGridView" AutoGenerateColumns="False" RowIndicatorVisibility="Collapsed" IsReadOnly="True" SelectionMode="Multiple" 
                             CanUserSelect="False" IsSynchronizedWithCurrentItem="False" ItemsSource="{Binding Mode=OneWay}" IsFilteringAllowed="True">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewSelectColumn x:Name="Select" IsResizable="False" />
                    <telerik:GridViewDataColumn Header="Status" DataMemberBinding="{Binding OrderStatusDescription}"/>
                    <telerik:GridViewDataColumn Header="Patient Name" DataMemberBinding="{Binding PatientName}"/>


If I change this:
<CheckBox Content="{Binding Header}"

TO this:
<CheckBox Content="{Binding WorklistGridView.Header}"

Then it will render the checkboxes, but there is no text beside them?

3 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 08 Mar 2011, 12:56 PM
Hi toddhd,

I have tested your XAMl. It seems the problem was in the select column missing a header. Then when trying to bind it in the listbox , the null value  caused an exception. (added an empty header for the select column).

Here is a fixed version of the XAML (tested and working).
<StackPanel x:Name="CustomizeGrid" Background="Transparent" Orientation="Horizontal">
    <ListBox ItemsSource="{Binding Columns, ElementName=WorklistGridView}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox  Content="{Binding Header}" IsChecked="{Binding IsVisible, Mode=TwoWay}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <telerik:RadGridView x:Name="WorklistGridView" AutoGenerateColumns="False" RowIndicatorVisibility="Collapsed" IsReadOnly="True" SelectionMode="Multiple" 
                     CanUserSelect="False" IsSynchronizedWithCurrentItem="False" ItemsSource="{Binding Mode=OneWay}" IsFilteringAllowed="True">
        <telerik:RadGridView.Columns>
            <telerik:GridViewSelectColumn Header=""  x:Name="Select" IsResizable="False" />
            <telerik:GridViewDataColumn Header="Status" DataMemberBinding="{Binding OrderStatusDescription}"/>
            <telerik:GridViewDataColumn Header="Patient Name" DataMemberBinding="{Binding PatientName}"/>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
</StackPanel>


All the best,
Pavel Pavlov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Lauren
Top achievements
Rank 1
answered on 02 Jun 2011, 11:55 PM
Hi,

How would you remove the Select Column from the listbox ?
For now i'm also binding the IsEnabled property of the checkbox to the IsREorderable property of the column so that the user cannot remove that column (assuming that's the only column which is not reorderable...). Is there a better way ?

I was also wondering how could i handle an additional checkbox to "Select All" ? or how to bind the checked status of a "Select All" checkbox to the listbox ones ?
and same with "Unselect All" ?
I'm trying to retrieve the checkboxes from the listbox and change their checked status accordingly but i'm having trouble actually retrieving those checkboxes.

Thank you for your help.
Laurence
0
Pavel Pavlov
Telerik team
answered on 06 Jun 2011, 09:59 AM
Hi Lauren,

Regarding your first question  - you should use a converter in the binding for the ItemsSource for the ListBox.
ItemsSource="{Binding Columns, ElementName=WorklistGridView}"

shouold look like :

ItemsSource="{Binding Columns, ElementName=WorklistGridView, Converter={StaticResource MyConverter}"

where My converter is a custom implemented IValueConverter. In teh convert method you sould filter the items ( columns) , so that you see only those needed.

Regarding your second question :
I would always recommend to not traverse visual elements when there is another way , so instead of taking the checkboxes ,
you can foreach the columns.
E.g. in the Checked event of the "Select all" checkbox, you can foreach the columns and set their IsVisible property.  Since we have two way binding , checkboxes in the listbox will automatically update .

If all this sounds obscure - let me know , and I will try to gather  a small sample for you.

All the best,
Pavel Pavlov
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
Tags
GridView
Asked by
toddhd
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Lauren
Top achievements
Rank 1
Share this question
or