propertygrid问题,StandardValuesCollection遍历在哪里实现?

chaomeier 2010-04-29 03:42:27
下面代码我是用于测试该控件下拉框的实现。我遇到的问题是,当原数据是string类型时,选择下拉框选项,不会出现问题,但当原数据是int类型时,会出现“类型“System.String”的对象无法转换为类型“System.Int32”。”的错误提示。

我重载了ConvertFrom函数,但只能修改StandardValuesCollection集合中的一个元素(例子里总共是8个元素:0,1,2。。。),不会改全部元素。
还有就是StandardValuesCollection的遍历是在哪里实现的?
请高手指点,急!!!(新手,分数不多,笑纳)

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);//自己些的sql类
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 = "小强";


}

}

}
...全文
231 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chaomeier 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dongyuechushi 的回复:]
对一个整形的属性和一个字符串的属性
使用一个相同的 TypeConverter,
这个还是头次见到.
[/Quote]
楼上,我在MyConverter类里,定义的是object数据,我想让它动态的,遇到某种类型自动转换成某种类型,这样不可以吗?请详细得说明下。谢谢!
chaomeier 2010-05-02
  • 打赏
  • 举报
回复
楼上,我在MyConverter类里,定义的是object数据,我想让它动态的,遇到某种类型自动转换成某种类型,这样不可以吗?请详细得说明下。谢谢!
yuanhuiqiao 2010-04-30
  • 打赏
  • 举报
回复
有些长,一时半会没找出问题,帮顶
dongyuechushi 2010-04-30
  • 打赏
  • 举报
回复
对一个整形的属性和一个字符串的属性
使用一个相同的 TypeConverter,
这个还是头次见到.

8,834

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧