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 <SPAN class=kwd>is</SPAN> bound to spread

    <SPAN class=kwd>public class</SPAN> MyClass
    {

    <SPAN class=kwd>private</SPAN> LongShort _longShortValueField;

    <SPAN class=kwd>public</SPAN> LongShort LongShortValue
    {
    <SPAN class=kwd>get</SPAN> { <SPAN class=kwd>return</SPAN> _longShortValueField; }
    <SPAN class=kwd>set</SPAN> { _longShortValueField = <SPAN class=kwd>value</SPAN>; }
    }
    }


    <SPAN class=kwd>public enum</SPAN> LongShort {

    Unknown,

    PositionLong,

    PositionShort,
    }



    The cell type LongShortCellType


    <SPAN class=kwd>internal class</SPAN> LongShortCellType : ComboBoxCellType
    {

    <SPAN class=kwd>public</SPAN> LongShortCellType()
    {
    SetLongShortDropDown();
    }

    <SPAN class=kwd>private void</SPAN> SetLongShortDropDown()
    {
    Clear();
    Items = System.Enum.GetNames(<SPAN class=kwd>typeof</SPAN>(LongShort));
    }




    <SPAN class=kwd>public override object</SPAN> GetEditorValue()
    {
    <SPAN class=kwd>object</SPAN> o = <SPAN class=kwd>null</SPAN>;

    <SPAN class=kwd>if</SPAN> ( <SPAN class=kwd>base</SPAN>.GetEditorValue() != <SPAN class=kwd>null</SPAN>)
    o= System.Enum.Parse(<SPAN class=kwd>typeof</SPAN>(LongShort), <SPAN class=kwd>base</SPAN>.GetEditorValue().ToString());

    <SPAN class=kwd>return</SPAN> o;
    }
    }

    Somehow the <SPAN class=kwd>value is</SPAN> never <SPAN class=kwd>set back</SPAN> to bound <SPAN class=kwd>object</SPAN>

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

    <FONT color=#ff0000>position.LongShortValue <SPAN class=kwd>is</SPAN> always Unknown</FONT>
     
  • 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.

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

    }

    <SPAN class=kwd>private void</SPAN> button1_Click(<SPAN class=kwd>object</SPAN> sender, EventArgs e)
    {
    MyClass[] ds = (MyClass[])fpSpread1.Sheets[0].DataSource;
    MyClass position = (MyClass) ds[0];

    }


    <SPAN class=kwd>public class</SPAN> MyClass
    {

    <SPAN class=kwd>private</SPAN> LongShort _longShortValueField;

    <SPAN class=kwd>public</SPAN> LongShort LongShortValue
    {
    <SPAN class=kwd>get</SPAN> { <SPAN class=kwd>return</SPAN> _longShortValueField; }
    <SPAN class=kwd>set</SPAN> { _longShortValueField = <SPAN class=kwd>value</SPAN>; }
    }
    }


    <SPAN class=kwd>public enum</SPAN> LongShort
    {

    Unknown,

    PositionLong,

    PositionShort,
    }

    <SPAN class=kwd>internal class</SPAN> LongShortCellType : FarPoint.Win.Spread.CellType.ComboBoxCellType
    {

    <SPAN class=kwd>public</SPAN> LongShortCellType()
    {
    SetLongShortDropDown();
    }

    <SPAN class=kwd>private void</SPAN> SetLongShortDropDown()
    {
    Clear();
    Items = System.Enum.GetNames(<SPAN class=kwd>typeof</SPAN>(LongShort));
    }




    <SPAN class=kwd>public override object</SPAN> GetEditorValue()
    {
    <SPAN class=kwd>object</SPAN> o = <SPAN class=kwd>null</SPAN>;

    <SPAN class=kwd>if</SPAN> (<SPAN class=kwd>base</SPAN>.GetEditorValue() != <SPAN class=kwd>null</SPAN>)
    o = System.Enum.Parse(<SPAN class=kwd>typeof</SPAN>(LongShort), <SPAN class=kwd>base</SPAN>.GetEditorValue().ToString());

    <SPAN class=kwd>return</SPAN> 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 = <SPAN class=kwd>typeof</SPAN> (MyClass).GetProperties();
    <SPAN class=kwd>foreach</SPAN> (PropertyInfo prop <SPAN class=kwd>in</SPAN> property)
    {
    ClassAttribute attrib =
    ClassAttribute.GetCustomAttribute(prop, <SPAN class=kwd>typeof</SPAN> (ClassAttribute)) <SPAN class=kwd>as</SPAN> ClassAttribute;
    sread1.ActiveSheet.BindDataColumn(attrib.DisplayOrder, <FONT color=#ff0000>prop.Name</FONT>);
    }
    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