How to create a CurrencyCellType which can have GradientHeaderRenderer effect too

Posted by: eager2knw on 8 September 2017, 1:31 pm EST

  • Posted 8 September 2017, 1:31 pm EST

    hi, 


    I have a cell.. It needs to be of currency cell type and it should have the GradientHeaderRenderer effect too. The cell is noneditable. But when calculations are performed in other cells , the values are reflected in this particular cell. can u help.


     Thanks,


    Eager2Knw

  • Replied 8 September 2017, 1:31 pm EST

    <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>Class1</FONT><FONT size=2>: FarPoint.Win.Spread.CellType.</FONT><FONT color=#2b91af size=2>CurrencyCellType

    </FONT><FONT size=2>

    {

    </FONT><FONT color=#0000ff size=2>

    #region</FONT><FONT size=2> Attributes


    </FONT><FONT color=#0000ff size=2>private</FONT><FONT size=2> </FONT><FONT color=#2b91af size=2>Color</FONT><FONT size=2> topColor = </FONT><FONT color=#2b91af size=2>Color</FONT><FONT size=2>.FromArgb(((</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>)(((</FONT><FONT color=#0000ff size=2>byte</FONT><FONT size=2>)(255)))),


    ((</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>)(((</FONT><FONT color=#0000ff size=2>byte</FONT><FONT size=2>)(255)))),


    ((</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>)(((</FONT><FONT color=#0000ff size=2>byte</FONT><FONT size=2>)(255)))));


    </FONT><FONT color=#0000ff size=2>private</FONT><FONT size=2> </FONT><FONT color=#2b91af size=2>Color</FONT><FONT size=2> bottomColor = </FONT><FONT color=#2b91af size=2>Color</FONT><FONT size=2>.FromArgb(((</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>)(((</FONT><FONT color=#0000ff size=2>byte</FONT><FONT size=2>)(136)))),


    ((</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>)(((</FONT><FONT color=#0000ff size=2>byte</FONT><FONT size=2>)(176)))),


    ((</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>)(((</FONT><FONT color=#0000ff size=2>byte</FONT><FONT size=2>)(227)))));


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

    </FONT><FONT color=#0000ff size=2>

    #endregion


     


     


    #region</FONT><FONT size=2> Properties


    </FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#2b91af size=2>Color</FONT><FONT size=2> ColorTop


    {


    </FONT><FONT color=#0000ff size=2>get</FONT><FONT size=2> { </FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2> topColor; }


    </FONT><FONT color=#0000ff size=2>set</FONT><FONT size=2> { topColor = </FONT><FONT color=#0000ff size=2>value</FONT><FONT size=2>; }


    }


    </FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#2b91af size=2>Color</FONT><FONT size=2> ColorBottom


    {


    </FONT><FONT color=#0000ff size=2>get</FONT><FONT size=2> { </FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2> bottomColor; }


    </FONT><FONT color=#0000ff size=2>set</FONT><FONT size=2> { bottomColor = </FONT><FONT color=#0000ff size=2>value</FONT><FONT size=2>; }


    }


    </FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> DecimalPlaces


    {


    </FONT><FONT color=#0000ff size=2>set</FONT><FONT size=2> { </FONT><FONT color=#0000ff size=2>value</FONT><FONT size=2> = decimalPlaces; }


    }


    </FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>bool</FONT><FONT size=2> FixedPoint


    {


    </FONT><FONT color=#0000ff size=2>set</FONT><FONT size=2> { </FONT><FONT color=#0000ff size=2>value</FONT><FONT size=2> = </FONT><FONT color=#0000ff size=2>true</FONT><FONT size=2>; }


    }

    </FONT><FONT color=#0000ff size=2>

    #endregion


     


     


    #region</FONT><FONT size=2> Constructor


    </FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> <FONT color=#2b91af>Class1</FONT>(</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> decimalPlaces)


    {


    </FONT><FONT color=#0000ff size=2>this</FONT><FONT size=2>.decimalPlaces = decimalPlaces;


    }

    </FONT><FONT color=#0000ff size=2>

    #endregion


    #region</FONT><FONT size=2> Events


    </FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><summary>

    </FONT><FONT size=2>

    </FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> Overrides the default paint cell method

    </FONT><FONT size=2>

    </FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2></summary>

    </FONT><FONT size=2>

    </FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><param name="g"></param>

    </FONT><FONT size=2>

    </FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><param name="r"></param>

    </FONT><FONT size=2>

    </FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><param name="appearance"></param>

    </FONT><FONT size=2>

    </FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><param name="value"></param>

    </FONT><FONT size=2>

    </FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><param name="isSelected"></param>

    </FONT><FONT size=2>

    </FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><param name="isLocked"></param>

    </FONT><FONT size=2>

    </FONT><FONT color=#808080 size=2>///</FONT><FONT color=#008000 size=2> </FONT><FONT color=#808080 size=2><param name="zoomFactor"></param>

    </FONT><FONT size=2>

    </FONT><FONT color=#0000ff size=2>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>override</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>void</FONT><FONT size=2> PaintCell(</FONT><FONT color=#2b91af size=2>Graphics</FONT><FONT size=2> g, </FONT><FONT color=#2b91af size=2>Rectangle</FONT><FONT size=2> r,


    FarPoint.Win.Spread.</FONT><FONT color=#2b91af size=2>Appearance</FONT><FONT size=2> appearance,


    </FONT><FONT color=#0000ff size=2>object</FONT><FONT size=2> value, </FONT><FONT color=#0000ff size=2>bool</FONT><FONT size=2> isSelected,


    </FONT><FONT color=#0000ff size=2>bool</FONT><FONT size=2> isLocked, </FONT><FONT color=#0000ff size=2>float</FONT><FONT size=2> zoomFactor)


    {


    </FONT><FONT color=#008000 size=2>//Set foreground and background colors with gradient

    </FONT><FONT size=2>

    </FONT><FONT color=#2b91af size=2>Brush</FONT><FONT size=2> backColorBrush = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> System.Drawing.Drawing2D.</FONT><FONT color=#2b91af size=2>LinearGradientBrush

    </FONT><FONT size=2>

    (r, topColor, bottomColor,


    System.Drawing.Drawing2D.</FONT><FONT color=#2b91af size=2>LinearGradientMode</FONT><FONT size=2>.Vertical);


    </FONT><FONT color=#2b91af size=2>Brush</FONT><FONT size=2> foreColorBrush = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#2b91af size=2>SolidBrush</FONT><FONT size=2>(appearance.ForeColor);


    </FONT><FONT color=#008000 size=2>//To set the alignment of the text

    </FONT><FONT size=2>

    </FONT><FONT color=#2b91af size=2>StringFormat</FONT><FONT size=2> sf = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#2b91af size=2>StringFormat</FONT><FONT size=2>();


    sf.Alignment = </FONT><FONT color=#2b91af size=2>StringAlignment</FONT><FONT size=2>.Center;


    </FONT><FONT color=#008000 size=2>//sf.LineAlignment = StringAlignment.Center;

    </FONT><FONT size=2>

    </FONT><FONT color=#008000 size=2>//Color the cell

    </FONT><FONT size=2>

    g.FillRectangle(backColorBrush, r);


    </FONT><FONT color=#008000 size=2>//Set format for a double value

    </FONT><FONT size=2>

    </FONT><FONT color=#0000ff size=2>if</FONT><FONT size=2> (value </FONT><FONT color=#0000ff size=2>is</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>double</FONT><FONT size=2>)


    {


    </FONT><FONT color=#008000 size=2>//g.DrawString((Convert.ToDecimal(value).ToString("N" + this.decimalPlaces)),

    </FONT><FONT size=2>

    </FONT><FONT color=#008000 size=2>// appearance.Font, foreColorBrush,

    </FONT><FONT size=2>

    </FONT><FONT color=#008000 size=2>// new RectangleF(r.X + r.Width - 35, r.Y, r.Width, r.Height));

    </FONT><FONT size=2>

    g.DrawString((</FONT><FONT color=#2b91af size=2>Convert</FONT><FONT size=2>.ToDecimal(value).ToString(</FONT><FONT color=#a31515 size=2>"N"</FONT><FONT size=2> + </FONT><FONT color=#0000ff size=2>this</FONT><FONT size=2>.decimalPlaces)),


    appearance.Font, foreColorBrush,


    </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#2b91af size=2>RectangleF</FONT><FONT size=2>(r.X, r.Y, r.Width, r.Height), sf);


    }


    </FONT><FONT color=#008000 size=2>//Dispose objects

    </FONT><FONT size=2>

    backColorBrush.Dispose();


    foreColorBrush.Dispose();


    }


     

    </FONT><FONT color=#0000ff size=2>

    #endregion


     


    This is my code .


    But The Dollar symbol does not appear. Pl help


    Thanks,


    Eager2knw

    </FONT>
  • Replied 8 September 2017, 1:31 pm EST

    Eager2knw,


    You need to format your number as a currency instead of a number in your DrawString code in the PaintCell method.

    <FONT size=2>

    g.DrawString((</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>Convert</FONT></FONT><FONT size=2>.ToDecimal(value).ToString(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"C"</FONT></FONT><FONT size=2> + </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>this</FONT></FONT><FONT size=2>.decimalPlaces)),

    </FONT>
  • Replied 8 September 2017, 1:31 pm EST

    Hi,


    For the same custom celltype ,


    I find that the exponential notation does not work for the cells that inherit this Custom celltype.


    I want the exponential form to be enabled


     


    Please help


    Thanks ,


    Eager2knw


     

  • Replied 8 September 2017, 1:31 pm EST

    You would have to create a custom celltype.  Here is an example of a NumberCellType that paints a gradient.  You can use it as an example...

    Class MyNumberGradient
       Inherits FarPoint.Win.Spread.CellType.NumberCellType
       Private topColor As Color = SystemColors.ControlLightLight
       Private bottomColor As Color = SystemColors.ControlDark


       Public Property ColorTop() As Color
          Get
             Return topColor
          End Get
          Set(ByVal value As Color)
             topColor = value
          End Set
       End Property

       Public Property ColorBottom() As Color
          Get
             Return bottomColor
          End Get
          Set(ByVal value As Color)
             bottomColor = value
          End Set
       End Property

      
    Public Overrides Sub PaintCell(ByVal g As Graphics, ByVal r As
    Rectangle, ByVal appearance As FarPoint.Win.Spread.Appearance, ByVal
    value As Object, ByVal isSelected As Boolean, ByVal isLocked As
    Boolean, ByVal zoomFactor As Single)
          Dim backColorBrush = New Drawing2D.LinearGradientBrush(r, topColor, bottomColor, Drawing2D.LinearGradientMode.Vertical)
          Dim foreColorBrush = New SolidBrush(appearance.ForeColor)
          g.FillRectangle(backColorBrush, r)
          If TypeOf value Is Double Then
             g.DrawString(CDbl(value), appearance.Font, foreColorBrush, New RectangleF(r.X, r.Y, r.Width, r.Height))
          End If
          backColorBrush.Dispose()
          foreColorBrush.Dispose()
       End Sub 'PaintCell
    End Class

  • Replied 8 September 2017, 1:31 pm EST

    Hello,


    Since you are painting the text in the cell when using this custom celltype, you need to paint it in the format you want it to display in the cell. Right now you are taking the value from the DataModel and drawing it into the cell with the format string of "C". This will take the value and try to format it as a currency value.

  • Replied 8 September 2017, 1:31 pm EST

    You would just need to call the Format method from within your PaintCell override to get the value formatted correctly. The only conditions that would not then work correctly and you would explicity need to check for are NegativeRed and NullDisplay.

     

Need extra support?

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

Learn More

Forum Channels