急求关于propertygrid下拉框问题的解决办法。。。在线等

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

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

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 = "小强";


}

}

}
...全文
186 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chaomeier 2010-04-29
  • 打赏
  • 举报
回复
之前我就提过一个问题,但是一直没回复的。。所以没结贴。。
q107770540 2010-04-29
  • 打赏
  • 举报
回复
mark

又见如此低的结帖率
chaomeier 2010-04-29
  • 打赏
  • 举报
回复
对了,里面的SqlTools类是我自己设计的sql类,只为了便于数据库操作

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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