简体   繁体   English

ComboBox.SelectedValue抛出空引用异常

[英]ComboBox.SelectedValue throws null reference exception

I've put together the following code to demonstrate a problem I'm having. 我把以下代码放在一起,以证明我遇到的问题。

It's a form with just a combobox, which is populated using an array generated from LINQ in the load method. 它只是一个只有一个组合框的表单,它使用在加载方法中从LINQ生成的数组填充。

It's got DisplayMember and ValueMember set. 它已设置DisplayMember和ValueMember。 Display member works as expected - it displays a list of numbers. 显示成员按预期工作 - 它显示数字列表。 However, as commented, SelectedValue is null. 但是,如评论所述,SelectedValue为null。

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DisplayMember = "Number";
    comboBox1.ValueMember = "Square";

    var it = from n in new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
             select new NumberAndSquare(n);
    comboBox1.Items.AddRange(it.ToArray());
}

private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
    ComboBox combo = sender as ComboBox;
    MessageBox.Show(combo.SelectedItem.ToString());  //works as expected
    MessageBox.Show(combo.SelectedValue.ToString()); //throws null reference exception
}

class NumberAndSquare
{
    public NumberAndSquare(int number)
    {
        Number = number;
    }

    public int Number
    { get; set; }
    public int Square
    {
        get
        {
            return Number*Number;
        }
    }

    public override String ToString()
    {
        return string.Format("{0}: {1}", Number, Square);
    }
}

What am I doing wrong? 我究竟做错了什么?

SelectedValue is no doubt null in this scenario because there is nothing being bound to it. 在这种情况下, SelectedValue无疑是null ,因为没有任何约束 AFAIK the DataMember / ValueMember properties are used only when you bind a DataSource to your combobox (which you aren't). AFAIK DataMember / ValueMember属性仅在将DataSource绑定到组合框时使用(您不是)。 For example, if you changed your code to: 例如,如果您将代码更改为:

var it = from n in new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
         select new NumberAndSquare(n);
comboBox1.DataSource = it.ToList();
comboBox1.DisplayMember = "Number";
comboBox1.ValueMember = "Square";

It should work 它应该工作

try this solution. 尝试此解决方案。 it has solved my same problem. 它解决了我同样的问题。

selectedvalue property is object data type. selectedvalue属性是对象数据类型。 but if you will use List with class or KeyValuePair, you must convert data type and then assign it selectedvalue property. 但是如果您将List与类或KeyValuePair一起使用,则必须转换数据类型,然后为其指定selectedvalue属性。

private void dogrulamaDoldur()
    {
        List<KeyValuePair<int, string>> l = new List<KeyValuePair<int, string>>();
        l.Add(new KeyValuePair<int, string>(0, "Parmak İzi, Şifre veya Kart"));
        l.Add(new KeyValuePair<int, string>(1, "Parmak İzi"));
        l.Add(new KeyValuePair<int, string>(2, "PIN"));
        l.Add(new KeyValuePair<int, string>(3, "Şifre"));
        l.Add(new KeyValuePair<int, string>(4, "Kart"));
        l.Add(new KeyValuePair<int, string>(5, "Parmak İzi veya Şifre"));
        l.Add(new KeyValuePair<int, string>(6, "Pamak İzi veya Kart"));
        l.Add(new KeyValuePair<int, string>(7, "Şifre veya Kart"));
        l.Add(new KeyValuePair<int, string>(8, "PIN ve Parmak İzi"));
        l.Add(new KeyValuePair<int, string>(9, "Parmak izi ve Şifre"));
        l.Add(new KeyValuePair<int, string>(10, "Parmak İzi ve Kart"));
        l.Add(new KeyValuePair<int, string>(11, "Şifre ve Kart"));
        l.Add(new KeyValuePair<int, string>(12, "Parmak İzi, Şifre ve Kart"));
        l.Add(new KeyValuePair<int, string>(13, "PIN, Parmak İzi ve Şifre"));
        l.Add(new KeyValuePair<int, string>(14, "Parmak İzi ve Kart veya Parmak İzi ve PIN"));
        CBdogrulama.DataSource = l;
        CBdogrulama.ValueMember = "Key";//important key is int data type
        CBdogrulama.DisplayMember = "Value"; 
    }

below incorrect 以下不正确

CBdogrulama.SelectedValue = g[2];//this line assign null value cannot g[2] CBdogrulama.SelectedValue = g [2]; //此行赋值空值不能为g [2]

below correct (below code assign value (solve null problem)) 低于正确(低于代码分配值(解决空问题))

CBdogrulama.SelectedValue = Convert.ToInt32(g[2]); CBdogrulama.SelectedValue = Convert.ToInt32(g [2]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM