当前位置:编程学习 > C#/ASP.NET >>

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

下面代码我是用于测试该控件下拉框的实现。我遇到的问题是,当原数据是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 = "小强";
            

        }

    }

}
--------------------编程问答-------------------- 有些长,一时半会没找出问题,帮顶 --------------------编程问答-------------------- 对一个整形的属性和一个字符串的属性
使用一个相同的 TypeConverter,
这个还是头次见到. --------------------编程问答-------------------- 楼上,我在MyConverter类里,定义的是object数据,我想让它动态的,遇到某种类型自动转换成某种类型,这样不可以吗?请详细得说明下。谢谢! --------------------编程问答--------------------
引用 2 楼 dongyuechushi 的回复:
对一个整形的属性和一个字符串的属性
使用一个相同的 TypeConverter,
这个还是头次见到.

楼上,我在MyConverter类里,定义的是object数据,我想让它动态的,遇到某种类型自动转换成某种类型,这样不可以吗?请详细得说明下。谢谢!
补充:.NET技术 ,  组件/控件开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,