手动的DataGridViewComboBoxCell设定值,值无效 [英] DataGridViewComboBoxCell setting value manually, value not valid

查看:211
本文介绍了手动的DataGridViewComboBoxCell设定值,值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码:

        private class Person
        {
            private string myName;
            private int myValue;

            public Person(string name, int value)
            {
                myName = name; 
                myValue = value;
            }
            public override string ToString()
            {
                return myName;
            }

            public string Name
            {
                get { return myName; }
                set { myName = value; }
            }

            public int Value
            {
                get { return myValue; }
                set { myValue = value; }
            }
        }



我用它来填充的DataGridViewComboBoxCell是这样的:

I use it to fill a DataGridViewComboBoxCell like this:

myDataGridViewComboBoxCell.ValueMember = "Value";
myDataGridViewComboBoxCell.DisplayMember = "Name";
myDataGridViewComboBoxCell.Items.Add(new Person("blabla", someNumber));



所有我想现在要做的就是选择一个人:

all I want to do now is to select a person:

myDataGridViewComboBoxCell.Value = someNumber;



但总是收到值无效误差。任何想法,为什么?
当我选择在我的程序中的项目,我可以看到正确的价值(someNumber),以便显示和ValueMember设置正确...

but keep getting "value is not valid"-error. Any Idea why? When I select an Item in my program I can see the right Value (someNumber) so Display and ValueMember are set correctly...

推荐答案

而不是增加 DataGridViewComboBoxColumn.Items ,你需要设置 DataGridViewComboBoxColumn.DataSource 来人的名单,并设置 DataGridViewComboBoxColumn.ValueType = typeof运算(人)

Rather than adding to DataGridViewComboBoxColumn.Items, you'll need to set DataGridViewComboBoxColumn.DataSource to a List of Person, and set DataGridViewComboBoxColumn.ValueType = typeof(Person).

应该这样做。虽然给予它已经两个月了,你问这个,你的问题可能已经失去了它的紧迫感。

Should do it. Although given it's been two months since you asked this, your problem may have lost it's sense of urgency.

这篇关于手动的DataGridViewComboBoxCell设定值,值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆