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

Problem when trying to reorder column

3 Answers 68 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lior
Top achievements
Rank 1
Lior asked on 02 Nov 2011, 02:33 PM
Hello,

I am trying to write a User Control which acts as a "column selector" for a RadGridView control.
My "ColumnSelector" control recieves a RadGridView control, and displays a list of the grid view's columns using a RadTreeView control. Next to the list I have two buttons, Up & Down, which allow the user to change the column order of the gridview.

The RadTreeView in the "ColumnSelector" control is binded to the RadGridView's Columns property, so that each change the user makes affects on the treeview the actual gridview.

In the up/down buttons event handler I need to reorder the columns, move the selected column one spot up or down.
I've tried using the GridView's ReorderColumns(old index, new index) method but it doesn't do anything. If someone could explain to me how to get it to work that'd be great.

Since ReorderColumns didn't work, I tried to make the swap myself:
//get the selected column
GridViewColumn column1 = Grid.Columns[selectedIndex];
  
 //remove the column at the original position
Grid.Columns.RemoveAt(selectedIndex);
  
//add the column at the new position
Grid.Columns.Insert(selectedIndex + 1, column1);

This causes an extra "blank" column to appear in the grid, between the real columns.

Has anyone ever seen this behavior? why is it happening?

Is there an example somewhere of how to implement such a "column selector" control? maybe my approach about the whole thing is incorrect..

This is how my treeview XAML looks like:
<nice:NiceTreeView ItemsSource="{Binding Columns, Converter={StaticResource columnsConverter}, Mode=TwoWay}"
                   IsEditable="True"
                   MinWidth="200" 
                   MaxHeight="380"
                   x:Name="itemsControl"
                   IsDragPreviewEnabled="False"
                   IsDragTooltipEnabled="False"
                   IsDropPreviewLineEnabled="False"
                   IsOptionElementsEnabled="True"
                   IsDragDropEnabled="True"
                   ItemsOptionListType="CheckList"
                   Checked="itemsControl_Checked"
                   Unchecked="itemsControl_UnChecked" 
                   ItemDoubleClick="itemsControl_ItemDoubleClick"
                   Edited="itemsControl_Edited"
                   ItemEditTemplate="{StaticResource editTemplate}">
</nice:NiceTreeView>

The ColumnsCoverter used converts between a GridViewColumnCollection (the type of RadGridView.Columns) and ObservableCollection<RadTreeViewItem>

Thanks,

Lior

3 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 02 Nov 2011, 02:54 PM
Hello Lior,

Please use the DisplayIndex property of the column .

All the best,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Lior
Top achievements
Rank 1
answered on 02 Nov 2011, 02:58 PM
Hi,
I tried using the DisplayIndex property, but it didn't work.
Also I think that changing the DisplayIndex while the column's IsVisible property is set to False might be related to the blank column issue.

Do you have an example that shows how to use it?

Thanks
0
Lior
Top achievements
Rank 1
answered on 03 Nov 2011, 10:13 AM
Hi,
I figured out how to use DisplayIndex but it doesn't suit my needs.
Changing the DisplayIndex property does change the way the columns are presented on the grid, but the actual order of the columns in the GridView's Columns property remains unchanged.
Meaning, the gird appears that "column B" is first and "column A" is second after I decrease column B's DisplayIndex by 1, but in the Columns property "column A" is still first and "column B" is second.

Is there a way to actually change the order of the columns so that my RadTreeView which is binded to Columns will reflect the change?

Thanks,

Lior
Tags
GridView
Asked by
Lior
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Lior
Top achievements
Rank 1
Share this question
or