Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1227
  • Last Modified:

Dynamically loaded items in combobox

hello experts,
i've got a problem using a combobox that should display dynamically loaded items. till now i only worked with comboboxes that items were loaded once at startup. i used the datasource-property that i linked to an IList consisting of objects. these objects had the following structure:

string Object.Display - the value that should be displayed by combobox
string Object.Value   - the key in the back to be used for further processing

i mapped the Display-property to comboboxes Displaymember-property and Value-property to Valuemember. Nice thing - did it well. But it does not work if you want to change the items of the combobox while working with it.

I remember having encountered the problem with listboxes, too. there i solved it forgetting the DataSource-property and using the old-style Items-property. Via the Items.Add-method i added all my items, via Items.Clear the collection was emptied to be filled with new data. That works well with Listboxes. But it seems not to be working with comboboxes?

does anyone of you know a nice solution to this problem?

thanks in advance.
0
kolpdc
Asked:
kolpdc
  • 10
  • 4
  • 2
  • +1
1 Solution
 
hammadrazaCommented:
Can you show some code?
0
 
somnaticCommented:
obj.Items.Clear();
obj.Items.Add();

works for ComboBoxes just as well ..
of course, no data binding ..
0
 
kolpdcAuthor Commented:

        /**
         * Fill a combobox.
         * Fill a combobox by adding the list to the ObjectCollection.
         *
         * @param p_Box combobox to be filled
         * @param p_List list to be filled into combobox
         * @return bool correctly filled?
         */
        private bool fillDynamicComboBox(
            ComboBox p_Box,
            IList          p_List)
        {
            if ((p_Box != null)  &&
                (p_List != null)) {

                p_Box.BeginUpdate();

                p_Box.DisplayMember = "Display";
                p_Box.ValueMember   = "Value";

                p_Box.Items.Clear();
                for(int i=0; i < p_List.Count; i++)
                    p_Box.Items.Add((CAddValue)p_List[i]);

                p_Box.EndUpdate();
            }
            else
                return false;

            return true;
        } //fillDynamicComboBox

        IList list = new ArrayList();
        //Filled with objects that have two public properties string Display, string Value

        fillDynamicComboBox(cboTest, list);
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
kolpdcAuthor Commented:
this version works fine, if you change the parameter "ComboBox p_Box" to "ListBox p_Box"
0
 
kolpdcAuthor Commented:
yes, normally it should work, but for in this case it does not seem to work. the collection is updated, but i am not able to access anything. SelectedValue, SelectedItem is always undefined, SelectedIndex is always -1 although i manually change the combobox's item choosen.

one item added to the Items-collection is a object of type CAddValue. the member-properties are mapped to the objects properties. so normally it should work. but it does not.
0
 
somnaticCommented:
call

ComboBox.Select() after you call .SelectedValue
0
 
kolpdcAuthor Commented:
unfortunately it does not work.

also debugger always shows that SelectedValue is undefined although one step before i gave it a value.
0
 
somnaticCommented:
can you show the code where the debugger shows you that the value is undefined ?
maybe 10 lines around that ?
0
 
kolpdcAuthor Commented:


   //The class used to hold my displaymember and the valuemember (key)
   public class CAddValue
   {
        private string m_Display;
        private string m_Value;
   
        public CAddValue(string Display, string Value) {
            m_Display = Display;
            m_Value = Value;
        }

        public string Display {
            get { return m_Display;  }
            set { m_Display = value; }
        }
        public string Value {
            get{ return m_Value;   }
            set { m_Value = value; }
        }

    } //CAddValue

   //******************************************************

        /*Only example - the static version where the box is only filled once. works well, but is not usable for dynamic reload of items*/
        /**
         * Fill a combobox.
         * Fill a combobox by adding the list to the ObjectCollection.
         *
         * @param p_Box combobox to be filled
         * @param p_List list to be filled into combobox
         * @return bool correctly filled?
         */
        private bool fillStaticCboBox(
            ComboBox p_Box,
            IList    p_List)
        {
            if ((p_Box != null)  &&
                (p_List != null) &&
                (p_Box.DataSource == null)) {

                p_Box.BeginUpdate();
                p_Box.DisplayMember = "Display";
                p_Box.ValueMember   = "Value";
                p_Box.DataSource = p_List;
                p_Box.EndUpdate();
            }
            else
                return false;

            return true;
        } //fillStaticCboBox

   //-----------------------------------------------------

        /* the dynamic version using items-collection. adding and removing elements works well. */
        /**
         * Fill a listbox.
         * Fill a listbox by adding the list to the ObjectCollection.
         *
         * @param p_Box listbox to be filled
         * @param p_List list to be filled into combobox
         * @return bool correctly filled?
         */
        private bool fillDynamicComboBox(
            ComboBox p_Box,
            IList    p_List)
        {
            if ((p_Box != null)  &&
                (p_List != null)) {

                p_Box.BeginUpdate();

                p_Box.DisplayMember = "Display";
                p_Box.ValueMember   = "Value";

                p_Box.Items.Clear();
                for(int i=0; i < p_List.Count; i++)
                    p_Box.Items.Add((CAddValue)p_List[i]);

                p_Box.EndUpdate();
            }
            else
                return false;

            return true;
        } //fillDynamicComboBox

   //-----------------------------------------------------

        /* This method fills CAddValue-objects from database and adds them to an IList*/
        /**
         * Fill list.
         * Fill list from a dblayer-manager.
         *
         * @param p_List list to be filled
         * @param p_Manager dblayer-manager
         */
        private void fillDBLayerList(
            IList                    p_List,
            IDBLayerManager p_Manager)
        {
            p_Manager.loadFromDb();

            p_List.Clear();
            p_List.Add(new CAddValue("", "0"));

            for(int i=0; i < p_Manager.Count(); i++) {
                p_List.Add(
                    new CAddValue(p_Manager.GetObject(i).ToString(), p_Manager.GetObject(i).GetKey().ToString()));
            }
        } //fillDBLayerList

   //-----------------------------------------------------
   //IList is filled
   fillDBLayerList(m_SlPrtnrList, m_CSlPrtnrMngr);

   //Combobox is filled with IList
   fillDynamicComboBox(cboSLPartner, m_SlPrtnrList);

   //-----------------------------------------------------
   //Later one item of the combobox shall be selected
   cboSLPartner.SelectedValue = p_obj.SLPartner.ToString();    //same as cboSLPartner.SelectedValue = "2";
0
 
kolpdcAuthor Commented:
that's the whole story.
0
 
somnaticCommented:
are u sure that your IList p_List is correctly filled ?
does the comboBox show the data lines ?
0
 
kolpdcAuthor Commented:
yes. it shows them. and i'm able to select an entry manually. only problem - i'm not able to get its value programmatically after the index changed.
0
 
AndreSteffensCommented:
Hi there!

After a few hours of nailbiting and hair-out-pulling: I think you need the following code:
      private void btnRemoveEntry_Click(object sender, System.EventArgs e)
      {
            cboTest.SelectedIndex=-1;
            cboTest.DisplayMember=String.Empty;
            cboTest.ValueMember=String.Empty;
            list.RemoveAt(5);//provide any other code to change the underlying datasource
            cboTest.DisplayMember="Display";
            cboTest.ValueMember="Value";
            cboTest.SelectedIndex=-1;
      }
You cannot Add to or Remove from the Items collection once you have set the Datasource. Funnily enough setting the Datasource to null, Clearing and reconnecting the DataSource doesn't work either.
You have to disconnect the Datasource by:
Clearing any selection (SelectedIndex=-1)
Clearing Display and Value Members
THEN change the DataSource (and not a moment before or all kinds of strange behaviour ensues)
set the Display and Value Members back to the properties they were before.
Deselecting again as the the combobox will give the impression that the user has selected an item while in fact the SelectedIndex=-1;
Hope this solves your problem!

André
0
 
kolpdcAuthor Commented:
AndreSteffens - great solution! works well! how did you find out?! try and error? i remember having searched for a long time before around a year. great solution - thanks.

(hope that behavior won't change depending on weather, light emission or a butterfly's moving wing...;)
0
 
kolpdcAuthor Commented:
for interested readers - here's the code-solution:

        /**
         * Fill a combobox.
         * Fill a combobox by adding the list to the ObjectCollection.
         *
         * @param p_Box combobox to be filled
         * @param p_List list to be filled into combobox
         * @return bool correctly filled?
         */
        private bool fillDynamicComboBox(
            ComboBox p_Box,
            IList    p_List)
        {
            if ((p_Box != null)  &&
                (p_List != null)) {

                p_Box.BeginUpdate();

                //Unlink values (using magic)
                p_Box.SelectedIndex   = -1;
                p_Box.DisplayMember = String.Empty;
                p_Box.ValueMember   = String.Empty;

                //Link values
                p_Box.DataSource      = p_List;
                p_Box.DisplayMember = "Display";
                p_Box.ValueMember   = "Value";
                p_Box.SelectedIndex   = -1;

                p_Box.EndUpdate();
            }
            else
                return false;

            return true;
        } //fillDynamicComboBox
0
 
AndreSteffensCommented:
Hi kolpc

Thanks for the compliment! Trial and error, or typing and cursing, typing and cursing, and even typing and cursing it was, but eternal bliss ensued! I see that you unlink and link the datasource after you change the datasource. For me that didn't work, I had to unlink first, then change, than link.

Regards,

André
0
 
kolpdcAuthor Commented:
it's nice to know that even logically-thinking persons that often live in 0-1-world are able to discover magic somewhen somewhere... ;)

like said - great thing. thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 10
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now