Override Sort Issue

2 posts, 1 answers
  1. Chuck
    Chuck avatar
    9 posts
    Member since:
    Oct 2012

    Posted 04 Feb 2015 Link to this post

    I am overriding my GetProperties() and Sort Methods of an objects PropertyDescriptorCollection in order to sort by index of a collection.

    In the ms winforms propertyGrid this is displaying as expected, while in the Telerik radPropertyGrid it is not performing the same as the picture depicts.

    Is there a reason this isn't compatible with the Telerik PropertyGrid?

    Below is the code for the PropertyDescriptorCollection sort and GetProperties overrides:

    class UserDataPropertyDescriptorCollection : PropertyDescriptorCollection
    {
        public UserDataPropertyDescriptorCollection(PropertyDescriptor[] propertyDescriptors)
            : base(propertyDescriptors)
        {
        }
     
        public override PropertyDescriptorCollection Sort()
        {
            PropertyDescriptorCollection ret = new PropertyDescriptorCollection(null);
            return this;
        }
     
        public override PropertyDescriptorCollection Sort(string[] names)
        {
            return this;
        }
     
        public override PropertyDescriptorCollection Sort(string[] names, System.Collections.IComparer comparer)
        {
            return this;
        }
     
        public override PropertyDescriptorCollection Sort(System.Collections.IComparer comparer)
        {
            return this;
        }
     
    }
     
    public class UserDataExpandableObjectConverter : ExpandableObjectConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            PropertyDescriptorCollection d = base.GetProperties(context, value, attributes);
            List<PropertyDescriptor> props = new List<PropertyDescriptor>();
     
            foreach (PropertyDescriptor f in d)
            {
                props.Add(f);
            }
     
            UserDataPropertyDescriptorCollection m = new UserDataPropertyDescriptorCollection(props.ToArray());
            return m;
        }
    }

    The object being displayed in the propertyGrid
    public class alpha
    {
        private UserDataCollection usd;
        private string test;
     
        public alpha()
        {
            usd = new UserDataCollection();
            test = "Fun";
        }
     
        [TypeConverterAttribute(typeof(UserDataExpandableObjectConverter))]
        [Category("Collection")]
        public UserDataCollection Collection
        {
            get { return usd; }
            set { usd = value; }
        }
     
        [Category("String")]
        public string Attr
        {
            get { return test; }
            set { test = value; }
        }
    }

    The form:

    public Form1()
    {
        InitializeComponent();
        UserDataCollection temp = new UserDataCollection();
        alpha obj = new alpha();
        //Inner test = new Inner();
     
        for (int i = 0; i < 25; i++)
        {
            obj.Collection.Add("");
        }
        radPropertyGrid1.SelectedObject = obj;
    }

    The only difference between the two projects is the propertyGrid
  2. Answer
    Dess
    Admin
    Dess avatar
    1609 posts

    Posted 09 Feb 2015 Link to this post

    Hello Chuck,

    Thank you for writing.

    Your question has already been answered in the support thread that you had opened on the same topic. However, I am posting the answer here as well in order the community to benefit from it.

    It is a known issue with RadPropertyGrid. However, its fix was introduced in Q3 2013 SP1. Here is the feedback item for your reference. I would recommend you to upgrade in order to benefit from all the introduced improvements. As to the specific version, you can use a custom IComparer which would only work in your specific case:
    public class MyComparer : IComparer<PropertyGridItem>
    {
        public MyComparer()
        {
        }
      
        public int Compare(PropertyGridItem x, PropertyGridItem y)
        {
            int xValue = -1;
            int yValue = -1;
      
            if (int.TryParse(x.Name.Substring(1), out xValue) &&
                int.TryParse(y.Name.Substring(1), out yValue))
            {
                if (xValue > yValue)
                {
                    return 1;
                }
                else if (xValue < yValue)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return x.Name.CompareTo(y.Name);
            }
        }
    }
    this.radPropertyGrid1.PropertyGridElement.PropertyTableElement.CollectionView.Comparer = new MyComparer();


    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Dess
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top