Confused about ComboBox and DataBinding

S

Steve K.

I'm surprised that I'm having trouble with this, I suspect that I'm having a
major brain fart and expecting something to work a way it doesn't.

In my example, I have a List<> of custom objects bound to a ComboBox.
I then have a single instance of the custom object which I have bound to the
SelectedItem property.

I'm expecting:
1) When the user changes the selection from the drop down the single, bound
instance will reference the selected item from the bound collection.
2) When binding the control to the single instance, it should select the
item in the list that matches (if there is one)

Here is a very simple example. I didn't include all the WinForm stuff, as
long as you have a ComboBox names comboBox1 it should be cut and paste.

<code>
List<SomeObject> _objects;
SomeObject _object;

public Form1()
{
InitializeComponent();

_objects = new List<SomeObject>();
_object = new SomeObject("C", 3);

_objects.Add(new SomeObject("A", 1));
_objects.Add(new SomeObject("B", 2));
_objects.Add(new SomeObject("C", 3));
_objects.Add(new SomeObject("D", 4));

comboBox1.DataSource = _objects;
comboBox1.ValueMember = "Value";
comboBox1.DisplayMember = "Display";

comboBox1.DataBindings.Add(new Binding("SelectedItem", _object,
"Value"));

comboBox1.SelectedIndexChanged += delegate(object sender, EventArgs e)
{
MessageBox.Show(string.Format("_object.Value = {0}\n_object.Display
= {1}", _object.Value, _object.Display));
};
}


public class SomeObject
{
private string _display;
private int _value;

public SomeObject(string display, int value)
{
_display = display;
_value = value;
}

public string Display
{
get { return _display; }
set { _display = value; }
}

public int Value
{
get { return _value; }
set { _value = value; }
}
}
</code>

If I change the code to use SelectedValue then it will select the correct
item in the list, however making a new selection in the list does not update
the _object reference OR it's Value property.

If anyone can straighten me out on this I'd really appreciate it, I'm going
in circles over here! ;0)

-Steve
 
D

DSK Chakravarthy

Steve,

All i understood from you is..
Point1) You are creating an entity as class
Point2) then you are assgining the entity to a list collection
point3.1) Created a event handler and delegated to an event of a ComboBox
Point3.2) trying to retrieve the data frrom the list collection depending on
the delegate
Point 4) expecting the value from the nth element of the collection base.

apart of giving you a solution for your corrent situation, i could recommend
you some thing with the above situation in mind.

Swap the points 1 and 2. Implement a class from CollectionBase and try
accessing the values. Hope that would solve your method.

Post back, if you need the code resolve the issue.

HTH
Chakravarthy
 
S

Steve K.

DSK Chakravarthy said:
Steve,

All i understood from you is..
Point1) You are creating an entity as class
Point2) then you are assgining the entity to a list collection
point3.1) Created a event handler and delegated to an event of a ComboBox
Point3.2) trying to retrieve the data frrom the list collection depending
on the delegate
Point 4) expecting the value from the nth element of the collection base.

apart of giving you a solution for your corrent situation, i could
recommend you some thing with the above situation in mind.

Swap the points 1 and 2. Implement a class from CollectionBase and try
accessing the values. Hope that would solve your method.

Post back, if you need the code resolve the issue.

HTH
Chakravarthy

Hi,

Thanks for the help.

I'm not sure I follow what you mean by switching #1 & #2?

Also, I know it's possible to bind a List<> to a control, what benefit would
I gain by using CollectionBase?

Thanks,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top