?? on PropertyGrid - Harold Demure

09-Jan-08 08:26:09
say i have the following in my property class (SelectedObject class)..

public enum TestList
{
GHI = 0,
DEF = 1,
ABC = 2,
}

public class PropsClass
{
private TestList _prop1;

public TestList Prop1
{
get { return _prop1; }
set { _prop1 = value; }
}

}

the enum displays ok in the grid for TestList but the values are listed in
order by the numeric value not the alphabetic name. with something more
complicated where the enum has maybe 30 unsorted values, it would be nice to
see the list sorted alphabetically not numerically just for readability's
sake. short of actually redoing the entire enum so everything is listed in
alphabetic and numeric order, is there any way to do this?? i looked into
creating a custom attribute but that lead me nowhere. nothing i read made it
clear whether it was even possible let alone how to implement it.
and i'm not referring to the PropertySort property of the grid since that
relates to the sorting of the property names, not the sorting of the values
of one particular property.
reply
 
 

?? on PropertyGrid - Bob Powell [MVP]

10-Jan-08 02:39:28
You should be able to implement your own TypeConverter based object for the
enum and get it to return standard values in the order you want.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
reply
 

?? on PropertyGrid - Harold Demure

10-Jan-08 05:52:39
yes.. i did read about the ITypeConverter (??) interface and how you have to
implement it but the term 'type converter' i didn't directly relate to
're-sort'. but i'll take another look. thanx..
reply
 

?? on PropertyGrid - Claes Bergefall

10-Jan-08 07:08:24
It's pretty easy actually. All you need to do is inherit a new class from
EnumConverter and override it's GetStandardValues method. In the
GetStandardValues method just create a list of your enum type, add the
values in the order you want them and then return the list.

Once you have that use the TypeConverterAttribute to attach the converter to
your enum.

/claes
reply
 

There is a simpler way - Robbe Morris - [MVP] C#

10-Jan-08 08:54:50
to create complex user interfaces with the PropertyGrid
control without monkeying with your classes.

http://www.eggheadcafe.com/tutorials/aspnet/270e9432-d236-47e7-b1af-5cd3abe27a75/net-propertygrid-control.aspx

--
Robbe Morris [Microsoft MVP - Visual C#]
AdvancedXL Server, Designer, and Data Analyzer
Convert cell ranges in Excel to rule driven web surveys
Free download:  http://www.equalssolved.com/default.aspx
reply
 

?? on PropertyGrid - Harold Demure

10-Jan-08 12:54:18
thanx for all the ideas..
reply
 

?? on PropertyGrid - Harold Demure

10-Jan-08 03:41:00
question on returning the new list.. the return value is a
StandardValuesCollection whose valueArray and values members are hidden.
since i assume i'd have to somehow update the svc object with my new list,
how would i do that with the necessary members not accessible??
i was psyched that i got this far but now i'm stuck on the most important
part.
reply
 

?? on PropertyGrid - Harold Demure

10-Jan-08 09:05:05
ok here's what i got.. maybe it wasn't what you had in mind but i felt i
wanted to physically sort the original enum as opposed to hardcoding a new
one in the right order. but anyway, this blows up with a {" is not a valid
value for TestList."} error. apparently it sees a null value somewhere. any
ideas?

namespace PropGridTest
{
[TypeConverter(typeof(MyEnumConverter))]
public enum TestList
{
GHI = 0,
ABC = 1,
DEF = 2,
}

public class PropsClass
{
private TestList _prop1;

public TestList Prop1
{
get { return _prop1; }
set { _prop1 = value; }
}

}

public class MyEnumConverter : EnumConverter, IComparer
{
public MyEnumConverter(Type type)
: base(type)
{
}

public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
string[] list = new string[3];

list = Enum.GetNames(typeof(TestList));

Array.Sort(list, this);

Values = new StandardValuesCollection(list);

return Values;
}

#region IComparer Members

public int Compare(object x, object y)
{
string sx = (string)x;
string sy = (string)y;

return sx.CompareTo(sy);
}

#endregion

}
}
reply
 

?? on PropertyGrid - Claes Bergefall

11-Jan-08 04:46:47
The returned list must contain values of your enum type (i.e. TestList), not
string. I'm guessing that's why it blows up. Try this:

public class TestListConverter : EnumConverter, IComparer<string>
{
public TestListConverter(Type type) : base(type)
{
}

public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
string[] names = Enum.GetNames(typeof(TestList));
Array.Sort<string>(names, this);

List<TestList> values = new List<TestList>();
foreach (string name in names)
values.Add((TestList)Enum.Parse(typeof(TestList), name));

return new
System.ComponentModel.TypeConverter.StandardValuesCollection(values);
}

public int Compare(string x, string y)
{
return String.Compare(x, y);
}
}

/claes
reply
 

?? on PropertyGrid - Harold Demure

11-Jan-08 06:35:13
awesome man.. it works.. thanx a bunch..
reply
 
Posting using webbrowser control
promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class