locked
ComboBox Binding to ArrayList not Refreshing with Add() RRS feed

  • Question

  • Hey All,

    I am having a problem getting a combobox to update with new values when added to the ArrayList that it is bound to.

    Initially it displays all items in myList fine.  It is later when I add items to myList that nothing happens.  Any Ideas?

    For example:

    //Array list of Categories
    ArrayList myList = new ArrayList();

    populateList(this.myList);

    //I have already created the combobox and set its properties

    this.cmbNewCategory.DataSource = this.myList;
    this.cmbNewCategory.DisplayMember = "getName";
    this.cmbNewCategory.ValueMember = "getID";
    this.cmbNewType.SelectedIndex = 0;

    public class Category
    {
    int id;
    string name;

    public Category(int id, string name)
    {
    this.id = id;
    this.name = name;
    }

    public int getID
    {
    get
    {
    return this.id;
    }
    set
    {

    }
    }
    public string getName
    {

    get
    {
    return this.name;
    }
    set
    {

    }
    }
    }


    Sunday, January 27, 2008 6:18 AM

Answers

  • Hi Kyle,

     

    For your concern, the work around way is to try to bind the CombBox to the ArrayList again when you update the datasource. Here are some sample codes for your reference:

    Code Snippet

    //Update myList

    UpdateList(this.myList);

    this.cmbNewCategory.DataSource = null;

    this.cmbNewCategory.DataSource = this.myList;
    this.cmbNewCategory.DisplayMember = "getName";
    this.cmbNewCategory.ValueMember = "getID";
    this.cmbNewType.SelectedIndex = 0;

     

    Hope this helps,

     

    Tuesday, January 29, 2008 7:28 AM

All replies

  • Hey there, no code for you but:

    "populateList(this.myList);" happens only once.  Your combobox is not bound to the "this.mylist" for on the fly changes.

    try "application.doevents();" or listbox.add(xxxx) or repopulate the with "populateList(this.mylist)";

    One of those will work.  If there is a  way to dynamically bind the "this.myList" to your combobox I have not not needed it or seen it yet.  Maybe a more senior programmer type can add a method to do this if it exists.

    good luck,

    SM
    Sunday, January 27, 2008 6:50 PM
  • Thank you for the reply SM.


    "populateList(this.myList);" happens only once.  Your combobox is not bound to the "this.mylist" for on the fly

    I thought that setting it's datasource to my ArrayList did just that.  Am i wrong in assuming this?  Also I think you are misunderstanding populateList(this.myList).  I am reading data from a text file and this method simply places this data into my arrayList initially when the program loads. 

    - Kyle

    Sunday, January 27, 2008 8:45 PM
  • Hi Kyle,

     

    For your concern, the work around way is to try to bind the CombBox to the ArrayList again when you update the datasource. Here are some sample codes for your reference:

    Code Snippet

    //Update myList

    UpdateList(this.myList);

    this.cmbNewCategory.DataSource = null;

    this.cmbNewCategory.DataSource = this.myList;
    this.cmbNewCategory.DisplayMember = "getName";
    this.cmbNewCategory.ValueMember = "getID";
    this.cmbNewType.SelectedIndex = 0;

     

    Hope this helps,

     

    Tuesday, January 29, 2008 7:28 AM