1

我整理了以下代码来演示我遇到的问题。

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

它设置了 DisplayMember 和 ValueMember。显示成员按预期工作 - 它显示数字列表。但是,正如评论的那样, SelectedValue 为空。

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);
    }
}

我究竟做错了什么?

4

3 回答 3

3

SelectedValue在这种情况下毫无疑问null,因为没有任何东西被绑定。AFAIK DataMember/ValueMember属性仅在您将 a 绑定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";

它应该工作

于 2013-01-17T12:40:55.243 回答
1

尝试这个:

comboBox1.DataSource = it.ToList();

按照这个链接:http: //msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.selectedvalue.aspx

于 2013-01-17T12:43:25.290 回答
0

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

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

下面不正确

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

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

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

于 2016-11-06T10:22:01.807 回答