property ordering in the the grid
thanks phild
thanks phild
Hi, PhilipDaniels,
I think the Category attribute maybe helpful for you in Categorized View,
however, in Alphabetical View, all the attributes are sorted by their first character of their name.
Hopes this helps,
Regards
Once again thanks for your feedback - its nice to know that somebody cares about my obscure problems
Rgds Phil D
I have my own class ValueGrid which inherits from PropertyGrid; it can order grid line items on properties, other than name, within my PropertyDescriptor derivative, but I cant use that in the IDE.
Hi, PhilipDaniels,
You really did a nice job.
I do agree with you that the only way to handle this is to write your own "PropertyGrid"
and to sort all the properties in your own method.
However, in my point of view, sorting the properties in alphabetical way inside Visual Studio is not a bad idea,
because it would be easier for the developers to find what they want from a bunch of names.
Besides, you could use other data conponents, such as ListBox, TreeView, DataGridView,
they can provide more friendly UI to your customers.
Finally, thanks for your concern on Microsoft products,
and we will take this into consideration to provide you better products in the future.
Regards
Yu Guo,
My users, medical research professionals, actually love the PropertyGrid paradigm whereas they detest DataGridView. My ValueGrid is a lot friendlier because of the Type Converters & UIEditors I've written, and because Tab/BackTab navigates up & down the value column - the name column only responds to context menu requests or keyboard equivalents. And the cost of change to the applications is often trivial compared wijh other UI paradigms.
Alphanumeric sort is fine if you know the name of the property for which your looking. E.G. The other day I knew that a control I was using had a word wrap property, but I it was hard to find - it was named EnableWordWrap (rather than simply "Word Wrap ?", via a DisplayNameAttribute) and it was in the Layout category.
Cheers PhilD
Hi Philip,
Here is some simple code for ordering your properties
public class PropertySorter : ExpandableObjectConverter
{
#region Methods
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
//
// This override returns a list of properties in order
//
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
ArrayList orderedProperties = new ArrayList();
foreach (PropertyDescriptor pd in pdc)
{
Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)];
if (attribute != null)
{
//
// If the attribute is found, then create an pair object to hold it
//
PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute;
orderedProperties.Add(new PropertyOrderPair(pd.Name, poa.Order));
}
else
{
//
// If no order attribute is specifed then given it an order of 0
//
orderedProperties.Add(new PropertyOrderPair(pd.Name, 0));
}
}
//
// Perform the actual order using the value PropertyOrderPair classes
// implementation of IComparable to sort
//
orderedProperties.Sort();
//
// Build a string list of the ordered names
//
ArrayList propertyNames = new ArrayList();
foreach (PropertyOrderPair pop in orderedProperties)
{
propertyNames.Add(pop.Name);
}
//
// Pass in the ordered list for the PropertyDescriptorCollection to sort by
//
return pdc.Sort((string[])propertyNames.ToArray(typeof(string)));
}
#endregion
}
#region Helper Class - PropertyOrderAttribute
[AttributeUsage(AttributeTargets.Property)]
public class PropertyOrderAttribute : Attribute
{
//
// Simple attribute to allow the order of a property to be specified
//
private int _order;
public PropertyOrderAttribute(int order)
{
_order = order;
}
public int Order
{
get
{
return _order;
}
}
}
#endregion
#region Helper Class - PropertyOrderPair
public class PropertyOrderPair : IComparable
{
private int _order;
private string _name;
public string Name
{
get
{
return _name;
}
}
public PropertyOrderPair(string name, int order)
{
_order = order;
_name = name;
}
public int CompareTo(object obj)
{
//
// Sort the pair objects by ordering by order value
// Equal values get the same rank
//
int otherOrder = ((PropertyOrderPair)obj)._order;
if (otherOrder == _order)
{
//
// If order not specified, sort by name
//
string otherName = ((PropertyOrderPair)obj)._name;
return string.Compare(_name, otherName);
}
else if (otherOrder > _order)
{
return -1;
}
return 1;
}
}
#endregion
[TypeConverter(typeof(PropertySorter))]
public class Car
{
[PropertyOrder(1)]
public string Ferrari
{
get { }
set { }
}
[PropertyOrder(2)]
public string Benz
{
get { }
set { }
}
[PropertyOrder(3)]
public string Camry
{
get { }
set { }
}
}
Just letting you know that your suggestion worked admirably on expandable properties.
However I can't fathom a convenient way to get it to work on "simple" properties of the target object, even though the properties are passed through exactly the same property ordering process. I can make it "work" if I panel beat all the properties into an unnatural expandable container property but it looks and feels ugly both externally and internally.
But at least properties within an expandable property look better without sequence numbers.
I'd also like to be able to do the same thing for categories - ie order them based on a non visible characterestic. But I'll settle for complete mastery of the order of properties.
Thanks for your help - rgds PhilD
An addendum - if I implement ICustomTypeDescriptor in a collection class and do the sort in the manner suugested then, PROVIDED I set the PropertyGrid.PropertySort property to Category (rather than CategoryAlphabetical) then the Sort order determined by the SortOrder attribute is honoured (ie the properties are presented in the same sequence as the array of display names).
But, how do I tell the IDE I want it to sort the properties in Category rather than CategoryAlphabetical - one really needs a third button to match the values of PropertySort.
I've replaced my own sequencing system in ValueGrid with the method suggested by Jogesh Grover, I dont know that it's better but at least its consistent with what I do to Expandable properties.
How does one coece MS into enhancing PropertyGrid with another PropertyTab, adding the PropertyOrder Attribute and implementing the Sort in GridItemCollection (I think that's the right place)
Rgds PhilD