Binding Spread to custom object

Posted by: matroskin-fp on 8 September 2017, 12:44 pm EST

  • Posted 8 September 2017, 12:44 pm EST

     
    MyClass is bound to spread

    public class MyClass
    {

    private LongShort _longShortValueField;

    public LongShort LongShortValue
    {
    get { return _longShortValueField; }
    set { _longShortValueField = value; }
    }
    }


    public enum LongShort {

    Unknown,

    PositionLong,

    PositionShort,
    }



    The cell type LongShortCellType


    internal class LongShortCellType : ComboBoxCellType
    {

    public LongShortCellType()
    {
    SetLongShortDropDown();
    }

    private void SetLongShortDropDown()
    {
    Clear();
    Items = System.Enum.GetNames(typeof(LongShort));
    }




    public override object GetEditorValue()
    {
    object o = null;

    if ( base.GetEditorValue() != null)
    o= System.Enum.Parse(typeof(LongShort), base.GetEditorValue().ToString());

    return o;
    }
    }

    Somehow the value is never set back to bound object

    BindingSource ds = (BindingSource) sprdEntry.Sheets[0].DataSource
    MyClass position = (MyClass) ds[e.Row];

    position.LongShortValue is always Unknown
     
  • Replied 8 September 2017, 12:44 pm EST

    Hello,


    I was not able to reproduce this issue. What release of Spread for Windows Forms are you using? Here is the code I used for testing.

     
       private void Form1_Load(object sender, EventArgs e)
    {
    MyClass[] x = new MyClass[] { new MyClass(), new MyClass() };
    fpSpread1.DataSource = x;
    fpSpread1.Sheets[0].Columns[0].CellType = new LongShortCellType();

    }

    private void button1_Click(object sender, EventArgs e)
    {
    MyClass[] ds = (MyClass[])fpSpread1.Sheets[0].DataSource;
    MyClass position = (MyClass) ds[0];

    }


    public class MyClass
    {

    private LongShort _longShortValueField;

    public LongShort LongShortValue
    {
    get { return _longShortValueField; }
    set { _longShortValueField = value; }
    }
    }


    public enum LongShort
    {

    Unknown,

    PositionLong,

    PositionShort,
    }

    internal class LongShortCellType : FarPoint.Win.Spread.CellType.ComboBoxCellType
    {

    public LongShortCellType()
    {
    SetLongShortDropDown();
    }

    private void SetLongShortDropDown()
    {
    Clear();
    Items = System.Enum.GetNames(typeof(LongShort));
    }




    public override object GetEditorValue()
    {
    object o = null;

    if (base.GetEditorValue() != null)
    o = System.Enum.Parse(typeof(LongShort), base.GetEditorValue().ToString());

    return o;
    }
    }

    }
     
  • Replied 8 September 2017, 12:44 pm EST

    I am using version 2.5

  • Replied 8 September 2017, 12:44 pm EST

    Hello,


    Make sure you are using the latest maintenance release of Spread for Windows Forms version 2.5. Did you try using my code to setup and get the change from the cell?

  • Replied 8 September 2017, 12:44 pm EST

    Thanks for help


    I found the problem


     

      PropertyInfo[] property = typeof (MyClass).GetProperties();
    foreach (PropertyInfo prop in property)
    {
    ClassAttribute attrib =
    ClassAttribute.GetCustomAttribute(prop, typeof (ClassAttribute)) as ClassAttribute;
    sread1.ActiveSheet.BindDataColumn(attrib.DisplayOrder, prop.Name);
    }
    instead of supplying property name I was supplying display name
    so it ws displaying everything correctly however would not bind properly
    Thanks again 
    Sorry for confusion
     
    
    
                                    
Need extra support?

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

Learn More

Forum Channels