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


    public class Class1: FarPoint.Win.Spread.CellType.CurrencyCellType


    {


    #region Attributes


    private Color topColor = Color.FromArgb(((int)(((byte)(255)))),


    ((int)(((byte)(255)))),


    ((int)(((byte)(255)))));


    private Color bottomColor = Color.FromArgb(((int)(((byte)(136)))),


    ((int)(((byte)(176)))),


    ((int)(((byte)(227)))));


    private int decimalPlaces;


    #endregion


     


     


    #region Properties


    public Color ColorTop


    {


    get { return topColor; }


    set { topColor = value; }


    }


    public Color ColorBottom


    {


    get { return bottomColor; }


    set { bottomColor = value; }


    }


    public new int DecimalPlaces


    {


    set { value = decimalPlaces; }


    }


    public new bool FixedPoint


    {


    set { value = true; }


    }


    #endregion


     


     


    #region Constructor


    public Class1(int decimalPlaces)


    {


    this.decimalPlaces = decimalPlaces;


    }


    #endregion


    #region Events


    ///


    /// Overrides the default paint cell method


    ///


    ///


    ///


    ///


    ///


    ///


    ///


    ///


    public override void PaintCell(Graphics g, Rectangle r,


    FarPoint.Win.Spread.Appearance appearance,


    object value, bool isSelected,


    bool isLocked, float zoomFactor)


    {


    //Set foreground and background colors with gradient


    Brush backColorBrush = new System.Drawing.Drawing2D.LinearGradientBrush


    (r, topColor, bottomColor,


    System.Drawing.Drawing2D.LinearGradientMode.Vertical);


    Brush foreColorBrush = new SolidBrush(appearance.ForeColor);


    //To set the alignment of the text


    StringFormat sf = new StringFormat();


    sf.Alignment = StringAlignment.Center;


    //sf.LineAlignment = StringAlignment.Center;


    //Color the cell


    g.FillRectangle(backColorBrush, r);


    //Set format for a double value


    if (value is double)


    {


    //g.DrawString((Convert.ToDecimal(value).ToString("N" + this.decimalPlaces)),


    // appearance.Font, foreColorBrush,


    // new RectangleF(r.X + r.Width - 35, r.Y, r.Width, r.Height));


    g.DrawString((Convert.ToDecimal(value).ToString("N" + this.decimalPlaces)),


    appearance.Font, foreColorBrush,


    new RectangleF(r.X, r.Y, r.Width, r.Height), sf);


    }


    //Dispose objects


    backColorBrush.Dispose();


    foreColorBrush.Dispose();


    }


     


    #endregion


     


    This is my code .


    But The Dollar symbol does not appear. Pl help


    Thanks,


    Eager2knw

  • 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.


    g.DrawString((Convert.ToDecimal(value).ToString("C" + this.decimalPlaces)),

  • 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