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

Properties order

4 Answers 18 Views
DataEntry
This is a migrated thread and some comments may be shown as answers.
Emin
Top achievements
Rank 1
Emin asked on 17 May 2018, 08:19 PM

Hello

I`ve the following class properties. But they are not orderd using Display-Order. Is it possible to order properties by another way?

[Display(Name = "Field2", Order = 2)]
public string Field2
{
    get; set;
}
 
[Display(Name = "Field3", Order = 3)]
public string Field3
{
    get; set;
}
 
[Display(Name = "Field1", Order = 1)]
public int Field1
{
    get; set;
}

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 May 2018, 11:44 AM
Hello, Emin,     

The RadSortOrderAttribute defines the order in which items would be ordered when no other ordering is applied (Alphabetical or Categorical alphabetical). The order can also be manipulated through the SortOrder property of PropertyGridItem. Setting the property would override the value from the attribute. 

public RadForm1()
{
    InitializeComponent();
    this.radPropertyGrid1.SelectedObject = new Item();
}
 
public class Item
{
    [DisplayName("Field2")]
    [RadSortOrder(2)]
    public string Field2 { get; set; }
 
    [DisplayName("Field3")]
    [RadSortOrder(3)]
    public string Field3 { get; set; }
 
    [DisplayName("Field1")]
    [RadSortOrder(1)]
    public int Field1 { get; set; }
}




I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Emin
Top achievements
Rank 1
answered on 21 May 2018, 12:28 PM

Thank you for reply Dess,

Just I wanted to order entries in the DataEntry control. RadSortOrder doens`t work in the DataEntry. Is any other solution for DataEntry?

 

0
Emin
Top achievements
Rank 1
answered on 21 May 2018, 12:35 PM

For a while I`m using solution which is working, but I don`t like it.

May be there is something better?

 

[AttributeUsage(AttributeTargets.Property)]
public class PropertyOrderAttribute : Attribute
{
    public PropertyOrderAttribute(int order) { Order = order; }
    public int Order { get; }
}
public class DataEntrySort<T> : BindingList<T>, ITypedList
{
    public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
    {
        PropertyDescriptorCollection typePropertiesCollection = TypeDescriptor.GetProperties(typeof(T));
        return typePropertiesCollection.Sort(new PropertyDescriptorComparer());
    }
    public string GetListName(PropertyDescriptor[] listAccessors)
    {
        return string.Format("A list with Properties for {0}", typeof(T).Name);
    }
}
public class PropertyDescriptorComparer : IComparer
{
    public int Compare(object x, object y)
    {
        if (x == y) return 0;
        if (x == null) return 1;
        if (y == null) return -1;
        PropertyDescriptor propertyDescriptorX = x as PropertyDescriptor;
        PropertyDescriptor propertyDescriptorY = y as PropertyDescriptor;
        PropertyOrderAttribute propertyOrderAttributeX = propertyDescriptorX?.Attributes[typeof(PropertyOrderAttribute)] as PropertyOrderAttribute;
        PropertyOrderAttribute propertyOrderAttributeY = propertyDescriptorY?.Attributes[typeof(PropertyOrderAttribute)] as PropertyOrderAttribute;
        if (Equals(propertyOrderAttributeX, propertyOrderAttributeY)) return 0;
        if (propertyOrderAttributeX == null) return 1;
        if (propertyOrderAttributeY == null) return -1;
        return propertyOrderAttributeX.Order.CompareTo(propertyOrderAttributeY.Order);
    }
}

 

public abstract class StudentAbstract
{
    [PropertyOrder(5)]
    public string Field0 { get; set; }
 
    [PropertyOrder(20)]
    public string Field2 { get; set; }
 
    [PropertyOrder(10)]
    public virtual string Field1 { get; set; }
 
    [PropertyOrder(30)]
    public int Field3 { get; set; }
}
public class Student : StudentAbstract
{
    [PropertyOrder(40)]
    public string Field4 { get; set; }
 
    [PropertyOrder(10)]
    public override string Field1 { get; set; }
 
    [PropertyOrder(50)]
    public int Field5 { get; set; }
}

 

private void Form1_Load(object sender, EventArgs e)
{
    radDataEntry1.DataSource = new DataEntrySort<Student> { new Student() };
}
 
private void radSaveClick(object sender, EventArgs e)
{
    Student returnValue = ((DataEntrySort<Student>)radDataEntry1.DataSource).FirstOrDefault();
}

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 22 May 2018, 09:04 AM
Hello, Emin,    

Please excuse me for the misunderstanding regarding RadPropertyGrid. I am glad that you have found a suitable solution for your case with RadDataEntry. To be honest, this is quite an elegant approach.

RadDataEntry allows changing the location of the automatically generated editor. A sample approach is demonstrated in the following help article: https://docs.telerik.com/devtools/winforms/dataentry/programmatically-arrange-items-

However, your approach is much better in my opinion.

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
DataEntry
Asked by
Emin
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Emin
Top achievements
Rank 1
Share this question
or