Running effect:
Solution:
Mainform. CS public partial class mainform: FORM {public mainform () {initializecomponent ();} private void form1_load (Object sender, eventargs e) {cprovincialcapital ProCap = new cprovincialcapital ("Nanjing ", 10000000); cprovince pro = new cprovince ("Jiangsu Province", ProCap, 8); cCountry cou = new Ccountry ("China", Pro); propertygrid1.selectedobject = cou ;}}
CCountry. cspublic class cCountry {public cCountry (string name, cprovince Pro) {_ province = pro; _ name = Name;} private string _ name; [displayname ("name")] public string name {get {return _ name;} set {_ name = value;} private cprovince _ province; [displayname ("Province")] public cprovince province {get {return _ province;} set {_ province = value ;}}}
Cprovince. CS [typeconverter (typeof (converterpro)] public class cprovince {public cprovince (string name, cprovincialcapital provincialcapital, int district) {_ name = Name; _ provincialcapital = provincialcapital; _ district = district;} private string _ name; [displayname ("name")] public string name {get {return _ name;} set {_ name = value ;}} private cprovincialcapital _ provincialcapital; [displayname ("provincial capital")] public cprovincialcapital provincialcapital {get {return _ provincialcapital;} set {_ provincialcapital = value ;}} private int _ district; [displayname ("region quantity")] public int district {get {return _ district;} set {_ district = value ;}}}
Cprovincialcapital. CS [typeconverter (typeof (converterprocap)] public class cprovincialcapital {public cprovincialcapital (string name, int population) {_ name = Name; _ population = population;} private string _ name; [displayname ("name")] public string name {get {return _ name;} set {_ name = value;} private int _ population; [displayname ("Population")] public int population {get {return _ population;} set {_ population = value ;}}}
Converterpro. cspublic class converterpro: expandableobjectconverter {// <summary> // returns whether the converter can use the specified context to convert the object to the specified type. /// </Summary> // <Param name = "context"> </param> // <Param name = "destinationtype"> </param> // <returns> </returns> Public override bool canconvertion (itypedescriptorcontext context, type destinationtype) {If (destinationtype = typeof (cprovince) {return true;} return base. canconvertize (context, destinationtype );} /// <summary> /// returns whether the specified context can be used to convert an object of the specified type to the type of the converter. /// </Summary> /// <Param name = "context"> provides the context of the component, container and attribute descriptor </param> /// <Param name = "sourcetype"> </param> /// <returns> </returns> Public override bool canconvertfrom (itypedescriptorcontext context, type sourcetype) {If (sourcetype = typeof (string) {return true;} return base. canconvertfrom (context, sourcetype );} /// <summary> /// use the specified context and culture information to convert the specified value object to the specified type. /// </Summary> /// <Param name = "context"> </param> // <Param name = "culture"> </param> // <Param name = "value"> </param> // /<Param name = "destinationtype"> </param> // <returns> </returns> Public override object Convertor (itypedescriptorcontext context, system. globalization. cultureinfo culture, object value, type destinationtype) {If (destinationtype = typeof (string) & value is cprovince) {cprovince pro = (cprovince) value; return pro. name + "," + pro. district + "," + pro. provincialcapital. name + "," + pro. provincialcapital. population. tostring ();} return base. convertize (context, culture, value, destinationtype);} public override object convertfrom (itypedescriptorcontext context, system. globalization. cultureinfo culture, object Value) {If (value is string) {string S = (string) value; string [] Ss = S. split (','); cprovincialcapital ProCap = new cprovincialcapital (ss [1]. tostring (), Int. parse (ss [2]); cprovince pro = new cprovince (ss [0]. tostring (), ProCap, Int. parse (ss [3]); Return pro;} return base. convertfrom (context, culture, value );}}
Converterprocap. CS public class converterprocap: expandableobjectconverter {// <summary> // returns whether the converter can use the specified context to convert the object to the specified type. /// </Summary> // <Param name = "context"> </param> // <Param name = "destinationtype"> </param> // <returns> </returns> Public override bool canconvertion (itypedescriptorcontext context, type destinationtype) {If (destinationtype = typeof (cprovincialcapital) {return true;} return base. canconvertize (context, destinationtype );} /// <summary> /// returns whether the specified context can be used to convert an object of the specified type to the type of the converter. /// </Summary> /// <Param name = "context"> provides the context of the component, container and attribute descriptor </param> /// <Param name = "sourcetype"> </param> /// <returns> </returns> Public override bool canconvertfrom (itypedescriptorcontext context, type sourcetype) {If (sourcetype = typeof (string) {return true;} return base. canconvertfrom (context, sourcetype );} /// <summary> /// use the specified context and culture information to convert the specified value object to the specified type. /// </Summary> /// <Param name = "context"> </param> // <Param name = "culture"> </param> // <Param name = "value"> </param> // /<Param name = "destinationtype"> </param> // <returns> </returns> Public override object Convertor (itypedescriptorcontext context, system. globalization. cultureinfo culture, object value, type destinationtype) {If (destinationtype = typeof (string) & value is cprovincialcapital) {cprovincialcapital cou = (cprovincialcapital) value; string name = cou. name; int population = cou. population; return name. tostring () + "," + population. tostring ();} return base. convertize (context, culture, value, destinationtype);} public override object convertfrom (itypedescriptorcontext context, system. globalization. cultureinfo culture, object Value) {If (value is string) {string S = (string) value; string [] Ss = S. split (','); cprovincialcapital cou = new cprovincialcapital (ss [0]. tostring (), Int. parse (ss [1]); Return cou;} return base. convertfrom (context, culture, value );}}
Address: http://www.cnblogs.com/2008freestyle/archive/2012/03/24/2415528.html