续……
------------------------------
private PropertyDescriptorCollection GetAllProperties()
{
if(privateProp == null)
{
privateProp = new PropertyDescriptorCollection(new
PropertyDescriptor[]{});
int i = 0; foreach(DataColumn myDC in myTable.Columns)
{
AddNewColumn myANC                    = new AddNewColumn(myDC,
categoryList[i].ToString(), (bool)dataTypeList[i]);
privateProp.Add(myANC);
PropertyDescriptorCollection myPDC    = myANC.GetChildProperties();
i++;
}
}
return privateProp;
} public class AddNewColumn : PropertyDescriptor
{
private DataColumn    myDC;
private string        insertCategory;
private Type   myType;
private bool isExpandable; public AddNewColumn(DataColumn thisDC, string insCat, bool
expandable):base(thisDC.ColumnName, new Attribute[]{})
{
myDC            = thisDC;
myType            = thisDC.GetType();
insertCategory    = insCat;
isExpandable    = expandable;
} public override System.Type ComponentType
{
get
{
return null;
}
} public override string Category
{
get
{
return insertCategory;
}
} public override bool IsReadOnly
{
get
{
return myDC.ReadOnly;
}
} public override System.Type PropertyType
{
get
{
if(isExpandable)
return myType;
else
return myDC.DataType;
}
} public override bool CanResetValue ( object component )
{
return true;
} public override object GetValue ( object component )
{
CustomClass myCC = component as CustomClass; if(myCC != null)
return myCC[myDC.ColumnName];
else
throw new ArgumentException ( "Component is not derived from DynamicProperties");
} public override void SetValue ( object component , object value )
{
CustomClass myCC = component as CustomClass; if(myCC != null)
myCC[myDC.ColumnName] = value;
else
throw new ArgumentException ( "Component is not derived from DynamicProperties" );
} public override void ResetValue ( object component )
{
CustomClass myCC = component as CustomClass; if(myCC != null)
myCC[myDC.ColumnName] = null;
else
throw new ArgumentException ( "Component is not derived from DynamicProperties" );
} public override bool ShouldSerializeValue ( object component )
{
return false;
} public override string Description
{
get
{
return myDC.Caption;
}
}
}
}
public class FileNameConverter: StringConverter 
{
public override bool GetStandardValuesSupported(
ITypeDescriptorContext context) 
{
return true;
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context) 
{
return new StandardValuesCollection(new string[]{"新文件", 
"文件1", 
"文档1"});
}
public override bool GetStandardValuesExclusive(
ITypeDescriptorContext context) 
{
return false;
} } 
}
-----------------------------------
用法:
private void button1_Click(object sender, System.EventArgs e)
{
TypeConverter converter = new TypeConverter();
FileNameConverter fc = new FileNameConverter();
 
CustomClass cc = new CustomClass();

DateTime now = DateTime.Now;
ArrayList al = new ArrayList();
al.Add("1");
al.Add("2");
string[] gender = new String[2];
gender[0] = "男";
gender[1] = "女";
al.Add("yuaiwu");
al.Add("于爱武");
bool b = true;

en  gen;

gen = en.男 ;
Hashtable table = new Hashtable();
table.Add(1,"yu");
table.Add(2,"ai");

int age = 0;
Color color = Color.FromArgb(255,0,0);
cc.AddProperty("姓 名","".GetType(),"my name","于爱武","个人信息",false);
cc.AddProperty("性 别",gen.GetType(),"my gender",gen,"个人信息",false);
cc.AddProperty("al",al.GetType(),"my gender",al,"个人信息",false);
cc.AddProperty("生 日",now.GetType(),"my birthday",now,"个人信息",false); cc.AddProperty("age",fc.GetType(),"age",fc,"a",false);
cc.AddProperty("公 司","".GetType(),"my gender","天津菲沃斯科技有限公司","公司信息",false);
cc.AddProperty("颜 色",color.GetType(),"my color",color,"公司信息",false);
cc.AddProperty("真 假",b.GetType(),"true/false",b,"true/false",false);

propertyGrid1.SelectedObject = cc;
}
-----------------------------------------
问题,一般情况下,上述情况工作良好,但是如果我的enum需要是灵活的,比如职业,教育程序待内容,是从数据库中取出的,而不象性别那样简单的写在enum中就可以了,上面不能处理,也许是我不会用,请大家帮忙

解决方案 »

  1.   

    参考.net的例子 ,有一个resourceTool,其源代码与此相似,至于下拉列表,可以不用Enum,MSDN上有一个篇文章介绍用法的,去查查吧
      

  2.   

    我 知道MSDN上那个例子,但是和这个情形不一样,那是把类写成固定的,而上面这个例子是活的,可以 动态的添属性
    resourceTool在哪/我没找到呀
      

  3.   

    做个TypeConverter并且指定它就可以了。