Databinding to a MultiColumnComboBoxCellType

Posted by: simoncousins on 8 September 2017, 1:00 pm EST

  • Posted 8 September 2017, 1:00 pm EST

    I have a spread that uses a MultiColumnComboBoxCellType to display a list of Products in the first column of each row.
    This works fine except when the BindingList of trade objects is bound to the spread the Product Id is displayed instead of the Product Name (e.g. 3 instead of Three).
    If I drop down the combobox then the Product name gets displayed as expected.
    How can I get the Product name to be displayed when the data is bound?
     
    public void BindData(BindingList trades)
    {
    _fpSpread.ActiveSheet.DataSource = trades;

    FarPoint.Win.Spread.CellType.MultiColumnComboBoxCellType mcb = new FarPoint.Win.Spread.CellType.MultiColumnComboBoxCellType();
    mcb.DataSourceList = BuildProductList();
    mcb.DataColumn = 0;
    mcb.ColumnEdit = 1;
    mcb.AutoSearch = AutoSearch.SingleCharacter;
    mcb.ButtonAlign = FarPoint.Win.ButtonAlign.Right;
    mcb.ListAlignment = FarPoint.Win.ListAlignment.Left;
    mcb.ListWidth = 100;
    mcb.ListOffset = 0;
    mcb.MaxDrop = 5;
    mcb.AcceptsArrowKeys = AcceptsArrowKeys.Arrows;

    _fpSpread.EditModeOn += new EventHandler(_fpSpread_EditModeOn);
    _fpSpread.ActiveSheet.Columns[0].CellType = mcb;
    }

    void _fpSpread_EditModeOn(object sender, EventArgs e)
    {
    FpSpread c = ((FarPoint.Win.Spread.CellType.GeneralEditor) _fpSpread.EditingControl).SubEditor as FpSpread;

    if (c != null)
    {
    c.Sheets[0].ColumnHeader.Columns[1].Label = "Product";
    c.Sheets[0].ColumnHeader.Columns[1].HorizontalAlignment = CellHorizontalAlignment.Left;
    c.Sheets[0].Columns[0].Visible = false;
    }
    }

    public IList BuildProductList()
    {
    List products = new List();
    products.Add(new Product("One", 1));
    products.Add(new Product("Two", 2));
    products.Add(new Product("Three", 3));
    return products;
    }
     	public class Product
    {
    private string _name;
    private int _id;

    public Product(string name, int id)
    {
    Name = name;
    Id = id;
    }

    public string Name
    {
    get { return _name; }
    set { _name = value; }
    }

    public int Id
    {
    get { return _id; }
    set { _id = value; }
    }
    }

    The trade class looks like this:

    public class Trade : ITrade

    {

    private int _id;

    private string _userID;

    private char _buySell;

    private DateTime? _startDeliveryDate;

    private DateTime? _endDeliveryDate;

    private double? _totalVolume;

    private int _unitOfMeasureID;

    private int? _productCodeID;

    ...

  • Replied 8 September 2017, 1:00 pm EST

    Hello,


    The way you have your combobox celltype object coded, the values from the bound DataSource for this column would need to be strings ("one", "two" or "three") for the item to show in the combobox. I believe you want to switch  the DataColumn and ColumnEdit proprties.

  • Replied 8 September 2017, 1:00 pm EST

    If I do as you suggest then the combo box list displays options "One", "Two" and "Three", but the bound/selected item is always displayed as the product id, i.e. 1, 2 or 3. This is not what I want.


    I want the options to be displayed as "One", "Two" and "Three" and the bound/selected item displayed as the product name, i.e. "One", "Two" or "Three".


    My code does this, but initially when the data is bound to the spread the combo box it incorrectly displays the product id that is bound to the cell, e.g. 1 rather than "One". When I click on the combo box's drop down it then correctly displays the product name corresponding to the bound product id e.g. "One".


    How can I get the data bind to display the name rather than the id?


     

  • Replied 8 September 2017, 1:00 pm EST

    Hello,


    It looks like the bound DataColumn is of type integer. If you want the value written to/from the cell with the bound column to be a string, you need to change the bound DataColumn to be of type string and hold the values "One", "Two" and "Three". I would think you want the values written from the Spread cell to the bound DataCOlumn to be integer and just display the text "One", "Two" or "Three" based on the selection.

  • Replied 8 September 2017, 1:00 pm EST

    Yes, I want the bound datacolumn to be an integer and the display to be the text "One", "Two" or "Three" based on the selection, but this does not happen when the data is bound.


    After assigning to the sheet's DataSource property the combobox displays the integer not the text.


    If I subsequently select an item from the combobox it does the right thing and displays the text.


    How do I get the data binding to display the text but still have the bound datacolumn an integer?

  • Replied 8 September 2017, 1:00 pm EST

    I have found a work-around from a previous post. The trick is to ensure the ColumnEdit property is set to the first column in the data assigned to the DataSourceList. To do this I had to create a DataTable from my IList and ensuring the first column was set to the Product.Description field.


    Very weird!

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels