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

Use ListBox to Manage GridView Columns

5 Answers 365 Views
GridView
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 03 Dec 2015, 02:48 PM

I am using the following listbox to manage visibility and order of the columns in my grid view. It works great for visibility (of course). If I drag a column in the listbox it reorders the grid columns fine. But if I reorder the grid columns in the grid it does not update the listbox. I am willing to disable column ordering in the grid view (and only reorder columns using the listbox) if I have to but would prefer not. I assume I can disable column reorder in the gridview anyway, still looking into it...

 Is there a trick I am missing to get the listbox to reorder when I update the column order from the gridview?

 

                <telerik:RadListBox x:Name="ColumnSelector" Margin="3" ItemContainerStyle="{StaticResource DraggableListBoxItem}" 
                                    ItemsSource="{Binding Columns,ElementName=GOOSEGrid}">
                    <telerik:RadListBox.DragDropBehavior>
                        <telerik:ListBoxDragDropBehavior />
                    </telerik:RadListBox.DragDropBehavior>
                    <telerik:RadListBox.DragVisualProvider>
                        <telerik:ScreenshotDragVisualProvider />
                    </telerik:RadListBox.DragVisualProvider>
                    <telerik:RadListBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Content="{Binding Header}" IsChecked="{Binding IsVisible}"/>
                        </DataTemplate>
                    </telerik:RadListBox.ItemTemplate>
                </telerik:RadListBox>

5 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 04 Dec 2015, 02:39 PM
Hello David,

Can you please take a look at the Reordering Columns topic? Basically, you can disable the reordering of RadGridView's columns by setting the CanUserReorderColumns property to "False".

As to your other requirement, a possible solution might be to use a ListView instead of ListBox and follow the approach demonstrated in the Changing ListView column order programatically forum thread.

I hope this helps.

Best Regards,
Stefan X1
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
David
Top achievements
Rank 1
answered on 10 Dec 2015, 06:06 PM

Yeah, disable column ordering was trivial but as I mentioned that is really an undesirable backup solution. I would really like to be able to reorder columns by dragging in the ListBox or dragging the column header. Dragging in the ListBox reorders the columns as desired. Dragging the column header does not update the listbox. The link below is a small example that shows this. Dragging column headers in the ListBox reorders the columns in the GridView but dragging columns in the GridView does not reorder the list.

https://drive.google.com/file/d/0B7XfJajUDQCNOGlfOHJzYkI4SFU/view?usp=sharing

0
Stefan
Telerik team
answered on 14 Dec 2015, 05:41 PM
Hi David,

I reviewed the provided sample application and I would like to point out couple of suggestions. Firstly, please note that RadGridView is a virtualized data-bound control and implementing logic depending on the visibility of its elements is something that we do not recommend. Having this in mind, the current logic for binding the IsChecked property of a CheckBox to the IsVisible property of GridViewDataColumn should be performed on data level. You can take a look at the UI Virtualization topic for more information.

As to your requirement, a possible approach would be to subscribe to the ColumnReordering event of RadGridView and implement custom logic for reordering the items of the ListBox.

Additionally, you can take a look at our documentation on DragDropManager.

I hope this helps.

Regards,
Stefan X1
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
David
Top achievements
Rank 1
answered on 16 Dec 2015, 02:13 PM

I had already modified my example to use an observable collection as in intermediary between the grid view's columns and the list box items. Obviously I set the list box's item source to my collection and then initialize it to the grid's columns. Then I handle the collection's CollectionChanged event and reorder the columns in the grid and I handle the grid's ReorderColumn event and update the collection accordingly. This seems to keep the list and column order in sync.

The visibility still works by binding the IsChecked to the IsVisible property of the column. IsVisible is a property of the column, not the underlying control (which I understand may or may not be present). I understand that binding to the Visibility of the control would not work but it seems like binding to IsVisible would be fine. It is exactly the technique used in the example here http://www.telerik.com/blogs/how-to-column-chooser-for-radgridview-for-silverlight-and-wpf.

If this is really a problem would it be sufficient to disable column virtualization. My grid will never have more than 20 or 30 columns but may have lots of rows. 

Thanks

Dave

 

0
Stefan
Telerik team
answered on 21 Dec 2015, 10:00 AM
Hi David,

Thank you for this clarification.

I made an investigation on this case and it seems that using the IsVisible property in this scenario would not affect the virtualization of the control, so please excuse me for the misunderstanding.

Also, even for a scenario with 20-30 columns, you can try setting the GroupRenderMode property of RadGridView to "Flat" for an additional performance boost. You can take a look at the Grouping Modes topic for more information.

Regards,
Stefan X1
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
David
Top achievements
Rank 1
Answers by
Stefan
Telerik team
David
Top achievements
Rank 1
Share this question
or