c# - Property Grid Number formatting -
is possible format numerical properties displayed in propertygrid of winforms?
class mydata { public int myprop {get; set;} } and want displayed in grid 1.000.000 example.
are there attributes this?
you should implement custom type converter integer property:
class mydata { [typeconverter(typeof(customnumbertypeconverter))] public int myprop { get; set; } } propertygrid uses typeconverter convert object type (integer in case) string, uses display object value in grid. during editing, typeconverter converts object type string.
so, need use type converter should able convert integer string thousand separators , parse such string integer:
public class customnumbertypeconverter : typeconverter { public override bool canconvertfrom(itypedescriptorcontext context, type sourcetype) { return sourcetype == typeof(string); } public override object convertfrom(itypedescriptorcontext context, cultureinfo culture, object value) { if (value string) { string s = (string)value; return int32.parse(s, numberstyles.allowthousands, culture); } return base.convertfrom(context, culture, value); } public override object convertto(itypedescriptorcontext context, cultureinfo culture, object value, type destinationtype) { if (destinationtype == typeof(string)) return ((int)value).tostring("n0", culture); return base.convertto(context, culture, value, destinationtype); } } result:
propertygrid.selectedobject = new mydata { myprop = 12345678 }; 
i recommend read getting out of .net framework propertygrid control msdn article understand how propertygrid works , how can customized.
Comments
Post a Comment