Reordering columns issue once again

3 posts, 0 answers
  1. Erik
    Erik avatar
    2 posts
    Member since:
    Apr 2016

    Posted 25 Apr Link to this post

    Hi,

    I'm facing one issue and I really do not know where could the problem.

     

    It's once again (as I read few other posts in the forum, nothing new) related to persisting order of columns in GridView. My problem happens only when the columns get reordered by drag-n-drop. When I add and or remove columns (this is done programatically in my view's code-behind) and try to persists Grid's state, everything works well. Unless I reorder their sequence.

     

     

    It's worth mentioning that I'm persisting the state in my filesystem so that I could restore it later. Solution with IsolatedStorageProvider is not well suited for me. But I do not think that this should be any issue.Storage happens everytime the button 'Save layout' is being clicked.

     

    I have created sample video that shows what I'm trying to say maybe even more expressively. Look here.I have created sample video that shows what I'm trying to say maybe even more expressively. Look here - https://drive.google.com/file/d/0B0cxhvgqPibkNHhyUzM1TVVHOVU/view

     

    I have implemented my version of ICustomPropertyProvider, but it's almost identical to the one provided in WPF Control Examples. See here:

     

    public class GridViewCustomPropertyProvider : ICustomPropertyProvider
        {

            private const string _columnsProperty = "Columns";
            private const string _sortDescriptorsProperty = "SortDescriptors";

            public CustomPropertyInfo[] GetCustomProperties()
            {
                return new CustomPropertyInfo[]
                {
                    new CustomPropertyInfo(_columnsProperty, typeof(List<ColumnProxy>)), 
                    new CustomPropertyInfo(_sortDescriptorsProperty, typeof(List<SortDescriptorProxy>)), 
                };
            }

            public void InitializeObject(object context)
            {
                if (context is RadGridView)
                {
                    RadGridView gridView = context as RadGridView;
                    //gridView.Columns.Clear();
                    gridView.SortDescriptors.Clear();
                    foreach (GridViewColumn gridViewColumn in gridView.Columns)
                    {
                        gridViewColumn.ClearFilters();
                    }
                }
            }

            public object InitializeValue(CustomPropertyInfo customPropertyInfo, object context)
            {
                return null;
            }

            public void RestoreValue(CustomPropertyInfo customPropertyInfo, object context, object value)
            {
                RadGridView gridView = context as RadGridView;

                switch (customPropertyInfo.Name)
                {
                    case _sortDescriptorsProperty:

                        gridView.SortDescriptors.SuspendNotifications();
                        gridView.SortDescriptors.Clear();

                        List<SortDescriptorProxy> proxies = value as List<SortDescriptorProxy>;

                        foreach (SortDescriptorProxy proxy in proxies)
                        {
                            GridViewColumn column = gridView.Columns[proxy.ColumnName];
                            gridView.SortDescriptors.Add(new ColumnSortDescriptor(){Column = column, SortDirection = proxy.SortDirection});
                        }

                        gridView.SortDescriptors.ResumeNotifications();

                        break;

                    default:

                        var columnProxies = value as List<ColumnProxy>;
                        //var orderedColumns = columnProxies.OrderBy(x => x.DisplayIndex);

                        foreach (ColumnProxy columnProxy in columnProxies)
                        {
                            GridViewColumn gridColumn = gridView.Columns[columnProxy.UniqueName];
                            gridColumn.DisplayIndex = columnProxy.DisplayIndex;
                            gridColumn.Header = "blabhalbl" + columnProxy.Header;
                            gridColumn.Width = columnProxy.Width;
                            //gridView.Columns.Add(gridColumn);
                        }

                        break;
                }
            }

            public object ProvideValue(CustomPropertyInfo customPropertyInfo, object context)
            {
                RadGridView gridView = context as RadGridView;

                var orderedColuns = OrderedColumns(gridView);

                switch (customPropertyInfo.Name)
                {

                    case _sortDescriptorsProperty:
                        {
                            List<SortDescriptorProxy> sortDescriptorProxies = new List<SortDescriptorProxy>();

                            foreach (ColumnSortDescriptor descriptor in gridView.SortDescriptors)
                            {
                                sortDescriptorProxies.Add(new SortDescriptorProxy()
                                {
                                    ColumnName = descriptor.Column.UniqueName,
                                    SortDirection = descriptor.SortDirection,
                                });
                            }

                            return sortDescriptorProxies;
                        }


                    default:

                        var columns = new List<ColumnProxy>();
                        foreach (GridViewColumn column in orderedColuns.OrderBy(x => x.DisplayIndex))
                        {
                            columns.Add(new ColumnProxy()
                            {
                                UniqueName = column.UniqueName,
                                Header = column.Header.ToString() + "someother",
                                DisplayIndex = column.DisplayIndex,
                                Width = column.Width
                            });
                        }

                        return columns;
                }
            }

            private static List<GridViewColumn> OrderedColumns(RadGridView gridView)
            {
                var orderedList = new List<GridViewColumn>();

                for (int i = 0; i < gridView.Columns.Count; i++)
                {
                    orderedList.Add(gridView.Columns[i]);
                }
                return orderedList;
            }
        }

     

    Thanks in advance for any piece of advice,

    Erik

  2. Erik
    Erik avatar
    2 posts
    Member since:
    Apr 2016

    Posted 26 Apr in reply to Erik Link to this post

    I also gave a chance to IsolatedStorageProvider, but the issue is the same though. Adding and removing columns works well. But rearranging them and then retrieving their state does not work properly.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Kiril Vandov
    Admin
    Kiril Vandov avatar
    324 posts

    Posted 28 Apr Link to this post

    Hello Erik,

    The attached providers looks ok and there is no reason on why the ordering state with drag drop is not saved. I have tested this scenario with our latest official binaries and its working.

    I have attached a sample project with CustomPropertyProvider for the GridView in which I am saving the FilterDescriptors/GroupDescriptors/SortDescriptors and Columns properties for your convenience.

    Kind regards,
    Kiril Vandov
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top