Update object values in ComboBox

Posted on 2007-10-15
Last Modified: 2010-04-15
Hi All,
I have, say, a class called Person with two properties: ID and Name, each object can add, update and delete itself to/from a database.  So if I add a bunch of these Person objects to a DropDownList box (in some kind of loop), that has its Value member set to ID and Display member set to Name - all works fine.  

I also have a textbox control that I use to enter either new or update the values for the Name property of each Person object.  All works fine except for one annoying thing when updating.  You can type the new Name value in the textbox and save it no problem, but how can I get the actual value in the DropDownList to also change its Name value without reloading the entire table?

Any ideas are much appreciated!
Question by:siskinds
    LVL 29

    Expert Comment

    I think there are at least two options.

    One is to store all objects in some kind of collection, and make a collection a datrasource for the dropdown list. In this case any aupadets to the content of the collection would be reflected automatically in the list.

    Another option is to find the element to be changed "manually" in the dropdown list and edit it's displayed value.

    Second one is easier to implement (if names are not unique - it might be a problem...).
    First one can be more tricky.
    LVL 22

    Accepted Solution

    If the textbox is populated by yourComboBox.Items(yourComboBox.SelectedIndex).Name before the edit,
    you can:
    yourComboBox.Items(yourComboBox.SelectedIndex).Name = newName;


    Author Comment

    Thanks for your input guys.

    Jim, you were close.  I had no access to the "Name", nor Text property of the Person object this way so simply replaced the currently selected object with the one I just modified and volia!
    myComboBox.Items[myComboBox.SelectedIndex] = newModifiedObject.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now