Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Confused about ComboBox and DataBinding

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve K. - 13 Mar 2008 09:05 GMT
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
DSK Chakravarthy - 13 Mar 2008 09:35 GMT
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

> 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.
[quoted text clipped - 75 lines]
>
> -Steve
Steve K. - 14 Mar 2008 02:36 GMT
> Steve,
>
[quoted text clipped - 16 lines]
> 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

>> 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
[quoted text clipped - 78 lines]
>>
>> -Steve

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.