RoadieRich
RoadieRich

Reputation: 6556

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.

It's got DisplayMember and ValueMember set. Display member works as expected - it displays a list of numbers. However, as commented, SelectedValue is 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?

Upvotes: 1

Views: 9852

Answers (3)

Fatih Topcu
Fatih Topcu

Reputation: 132

try this solution. it has solved my same problem.

selectedvalue property is object data type. but if you will use List with class or KeyValuePair, you must convert data type and then assign it selectedvalue property.

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]

below correct (below code assign value (solve null problem))

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

Upvotes: 0

James
James

Reputation: 82096

SelectedValue is no doubt null in this scenario because there is nothing being bound to it. AFAIK the DataMember/ValueMember properties are used only when you bind a DataSource to your combobox (which you aren't). 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

Upvotes: 3

phnkha
phnkha

Reputation: 7872

try this:

comboBox1.DataSource = it.ToList();

Follow this link: http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.selectedvalue.aspx

Upvotes: 1

Related Questions