110,538
社区成员
发帖
与我相关
我的任务
分享
namespace attributetest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
propertyGrid1.SelectedObject = new MyObject();
}
}
public class MyConverter : ExpandableObjectConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
ListAttribute lst = (ListAttribute)context.PropertyDescriptor.Attributes[typeof(ListAttribute)];
StandardValuesCollection vals = new TypeConverter.StandardValuesCollection(lst.obcollection);
return vals;
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return true;
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
return value;
}
}
public class ListAttribute : Attribute
{
public object[] obcollection;
public ListAttribute()
{
}
public ListAttribute(string str)
{
int i=0;
string conn="Data Source=(local);Initial Catalog=yj;Integrated Security=True;Pooling=False";
SQLTools sql=new SQLTools(conn);
DataTable dt=sql.executeQuery(str);
obcollection=new object[dt.Rows.Count];
MessageBox.Show(dt.Rows[0][0].GetType().ToString());
foreach(DataRow row in dt.Rows)
{
obcollection[i]=row[0];
i++;
}
}
}
public class MyObject
{
private int id;
[TypeConverter(typeof(MyConverter)), ListAttribute("select bdz_id from dds_biandianzhan")]
public int ID
{
get
{
return id;
}
set
{
id = value;
}
}
private string code;
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
private string name;
[TypeConverter(typeof(MyConverter)), ListAttribute("select bdz_name from dds_biandianzhan")]
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public MyObject()
{
this.id = 0;
this.code = "小旺财";
this.name = "小强";
}
}
}