c# combobox binding to list of objects
Asked Answered
R

2

19

Is it possible to bind a ComboBox to a list of objects, but have the selectedvalue property point to the object, not a property of the object?

I only ask because we have some Business Objects which have references to other objects - such as a 'Year' object. That year object may need to be switched out for another year object.


Only solution I can come up with is to have another class with a single property, in this case pointing to a year object. then bind the combobox to a List of these and set both the display and value members to the single property.

But doing that for any 'lookups' we have seems like a bit of a pain??

Rear answered 22/9, 2010 at 10:28 Comment(0)
B
31

If you set the ValueMember to null the selected value will always be the object, not a property:

{
    public class TestObject
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
    public partial class Form1 : Form
    {
        private System.Windows.Forms.ComboBox comboBox1;

        public Form1()
        {
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(23, 13);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 21);
            this.comboBox1.TabIndex = 0;
            this.comboBox1.SelectedValueChanged += new System.EventHandler(this.comboBox1_SelectedValueChanged);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.comboBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

            BindingList<TestObject> objects = new BindingList<TestObject>();
            for (int i = 0; i < 10; i++)
            {
                objects.Add(new TestObject() { Name = "Object " + i.ToString(), Value = i });
            }
            comboBox1.ValueMember = null;
            comboBox1.DisplayMember = "Name";
            comboBox1.DataSource = objects;
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
                TestObject current = (TestObject)comboBox1.SelectedValue;
                MessageBox.Show(current.Value.ToString());
            }
        }
    }
}
Boycott answered 22/9, 2010 at 10:38 Comment(1)
If I use this approach I can't programatically set the SelectedValue, I had to use SelectedItem.Sphinx
E
6

You can bind a ComboBox to any list of values using the DataSource property. Or actually:

An object that implements the IList interface, such as a DataSet or an Array. The default is null.

You then use the ValueMember to control what you get from SelectedValue. Settings this to null as jmservera writes lets you get the object as it is in the DataSource.

Esperanzaespial answered 22/9, 2010 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.