ComboBox.SelectedValue抛出空引用异常

时间:2013-01-17 12:31:23

标签: c# .net winforms

我已将以下代码放在一起,以演示我遇到的问题。

这是一个只有一个组合框的表单,它使用在加载方法中从LINQ生成的数组填充。

设置了DisplayMember和ValueMember。显示成员按预期工作 - 它显示数字列表。但是,如评论所述,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);
    }
}

我做错了什么?

3 个答案:

答案 0 :(得分:3)

在这种情况下,

SelectedValue无疑是null,因为没有任何绑定。 AFAIK DataMember / ValueMember属性仅在您将DataSource绑定到组合框(您不是)时使用。例如,如果您将代码更改为:

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";

应该有效

答案 1 :(得分:1)

试试这个:

comboBox1.DataSource = it.ToList();

请点击此链接:http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.selectedvalue.aspx

答案 2 :(得分:0)

试试这个解决方案。它解决了我同样的问题。

selectedvalue属性是对象数据类型。但如果您将List与class或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"; 
    }

低于错误

CBdogrulama.SelectedValue = g [2]; //此行赋值空值不能为g [2]

低于正确(低于代码分配值(解决空问题))

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