Combobox items update problem

Mar 17, 2009 at 12:21 PM
Hi,

I implemented a simple application that uses combobox-cell editor in sourcegrid. To implement proper display/id behavior in combobox-cell, the value list is a List<ComboboxItem> type object and set combobox.StandardValues property to populate. ComboboxItem is a simple class with 2 string properties and overrides ToString() method.

After form opens, i cannot add new items to the combobox items dynamically (for example user clicks add). I can add but now shown in combobox dropdown item list. I think combobox-cell populates all data before form initialization and after grid shown it is not possible to edit items.

Is there a way to update/add/delete items of the combobox-cell in grid at runtime?

Thanks,

Mack
Coordinator
Mar 18, 2009 at 6:27 AM
Hi, Mack,

Strange behaviour, nothing should block you from adding new items to combobox after it is added to grid, or any other time.
Maybe combobox cell makes a copy of your combo-box, and then you are updating the wrong one combobox?
Mar 19, 2009 at 6:37 AM
Edited Mar 19, 2009 at 6:40 AM
hey everyone,

I solved the problem.I am not updating the wrong combobox but combobox object doesnot update the items in an expected way (not expected for me:))

for example: (code may not compile but showed you to get the idea)

public class ComboboxItem
{
    public string Display;
    public int Data;

    public ComboboxItem ( string display , int data )
    {
        Display = display;
        Data = data;
    }

    public overide string Object.ToString ( )
    {
        return Display;
     }
}



public class XYZ {

public List<ComboBoxItem> objectList;
public SourceGrid.Cells.Editors.ComboBox mComboBoxEditor;


public XYZ ( ) // constructor
{
    // add some items to objectList lets say "One"|1 , "Two"|2 , "Three"|3 are added here
    .....

    mComboBoxEditor.StandardValues = objectList; // mComboBoxEditor shows 3 items correctly
}

// user clicks a button in UI
public void btnAddNew_Click ( object o , EventArgs e )
{
    // #1 combobox doesnot update and show "Four"
    objectList.Add( new ComboboxItem( "Four" , 4 ) ); // after this point objectList has 4 items ("One"|1 , "Two"|2 , "Three"|3 , "Four"|4)

    // #2 combobox doesnot update and show "Four", doesnot even crash
    (mComboBoxEditor.StandardValues as List<ComboboxItem>).Add( new ComboboxItem( "Four" , 4 ) );

    // #3 combobox doesnot update and show "Four"
    mComboBoxEditor.StandardValues = objectList;
   
    // #4 this works and currently using this "ugly" method :)
    mComboBoxEditor.StandardValues = null;
    mComboBoxEditor.StandardValues = objectList;
}
}


I hope you get the idea how i solved. I think Combobox makes copy of items when .StandardValues property is set. Re-assignning doesnot work too (#3)
Also I was wrong about "doesnot work after grid is shown", sorry about the confusion.

thanks
Coordinator
Mar 20, 2009 at 6:03 AM
Ok!