Databinding to a MultiColumnComboBoxCellType

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

  • Posted 8 September 2017, 1:00 pm EST

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

    FarPoint.Win.Spread.CellType.MultiColumnComboBoxCellType mcb = <SPAN class=kwd>new</SPAN> 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 += <SPAN class=kwd>new</SPAN> EventHandler(_fpSpread_EditModeOn);
    _fpSpread.ActiveSheet.Columns[0].CellType = mcb;
    }

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

    <SPAN class=kwd>if</SPAN> (c != <SPAN class=kwd>null</SPAN>)
    {
    c.Sheets[0].ColumnHeader.Columns[1].Label = <SPAN class=st>"Product"</SPAN>;
    c.Sheets[0].ColumnHeader.Columns[1].HorizontalAlignment = CellHorizontalAlignment.Left;
    c.Sheets[0].Columns[0].Visible = <SPAN class=kwd>false</SPAN>;
    }
    }

    <SPAN class=kwd>public</SPAN> IList BuildProductList()
    {
    List products = <SPAN class=kwd>new</SPAN> List();
    products.Add(<SPAN class=kwd>new</SPAN> Product(<SPAN class=st>"One"</SPAN>, 1));
    products.Add(<SPAN class=kwd>new</SPAN> Product(<SPAN class=st>"Two"</SPAN>, 2));
    products.Add(<SPAN class=kwd>new</SPAN> Product(<SPAN class=st>"Three"</SPAN>, 3));
    <SPAN class=kwd>return</SPAN> products;
    }
    </PRE><PRE class=coloredcode>  <SPAN class=kwd>public class</SPAN> Product
    {
    <SPAN class=kwd>private string</SPAN> _name;
    <SPAN class=kwd>private int</SPAN> _id;

    <SPAN class=kwd>public</SPAN> Product(<SPAN class=kwd>string</SPAN> name, <SPAN class=kwd>int</SPAN> id)
    {
    Name = name;
    Id = id;
    }

    <SPAN class=kwd>public string</SPAN> Name
    {
    <SPAN class=kwd>get</SPAN> { <SPAN class=kwd>return</SPAN> _name; }
    <SPAN class=kwd>set</SPAN> { _name = <SPAN class=kwd>value</SPAN>; }
    }

    <SPAN class=kwd>public int</SPAN> Id
    {
    <SPAN class=kwd>get</SPAN> { <SPAN class=kwd>return</SPAN> _id; }
    <SPAN class=kwd>set</SPAN> { _id = <SPAN class=kwd>value</SPAN>; }
    }
    }</PRE><PRE class=coloredcode>
    <FONT face="Times New Roman">The trade class looks like this:</FONT></PRE><PRE class=coloredcode><FONT size=2><P></FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>class</FONT><FONT size=2> </FONT><FONT color=#2b91af size=2>Trade</FONT><FONT size=2> : </FONT><FONT color=#2b91af size=2>ITrade

    </FONT><FONT size=2>

    {

    </FONT><FONT color=#0000ff size=2> private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> _id;

    </FONT><FONT color=#0000ff size=2> private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2> _userID;

    </FONT><FONT color=#0000ff size=2> private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>char</FONT><FONT size=2> _buySell;

    </FONT><FONT color=#0000ff size=2> private</FONT><FONT size=2> </FONT><FONT color=#2b91af size=2>DateTime</FONT><FONT size=2>? _startDeliveryDate;

    </FONT><FONT color=#0000ff size=2> private</FONT><FONT size=2> </FONT><FONT color=#2b91af size=2>DateTime</FONT><FONT size=2>? _endDeliveryDate;

    </FONT><FONT color=#0000ff size=2> private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>double</FONT><FONT size=2>? _totalVolume;

    </FONT><FONT color=#0000ff size=2> private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> _unitOfMeasureID;

    </FONT><FONT color=#0000ff size=2> private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>? _productCodeID;</FONT>

    <FONT size=2>...

    </FONT></PRE>
  • 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<Product> 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