NumberCellType to 6 decimal places but don't show trailing 0's

Posted by: scottp on 4 September 2023, 10:31 am EST

    • Post Options:
    • Link

    Posted 4 September 2023, 10:31 am EST

    Hi,

    I’m trying to achieve this (seemingly reasonable) behavior:

    A cell can have a minimum of 2 decimal places and a maximum of 6 decimal places. Trailing zeros are not shown. Entering and then leaving the cell without changing anything should not fire a change event.

    Any help in the form of code or instruction will be appreciated.

    Regards,

    Scott

  • Posted 4 September 2023, 9:45 pm EST

    Hi Scott,

    You can set the NumberFormat property of the cells for which you want to set a minimum of 2 decimal places and a maximum of 6 decimal places, as shown in the following code:

    fpSpread1.ActiveSheet.AsWorksheet().Cells["A1:A10"].NumberFormat = "0.00####";

    Please refer DecimalPointNumberCell.zip for full implementation.

    Entering and then leaving the cell without changing anything should not fire a change event.

    As per our understanding, you do not want the CellChanged event to get fired when cell value is not changed. We did not find any issue in the behavior of this event on our end. Please check CellChanged.zip showing the behavior at our end.

    Kindly let us know if your requirement differs from our understanding.

    Attachments:

    DecimalPointNumberCell.zip

    CellChanged.zip

    Thanks & Regards,

    Aastha

  • Posted 7 September 2023, 4:50 pm EST

    Thank you for the sample code. The code works as-is, but does not work with my code because my CellType is set to FarPoint.Win.Spread.CellType.NumberCellType.

    How can I achieve the desired behavior with a NumberCellType?

  • Posted 10 September 2023, 11:36 pm EST

    Hi Scott,

    You can create a custom NumberCellType cell and define its NumberFormat as shown in the following lines of code:

    public class NumberCellType2 : NumberCellType
        {
          private FpSpread _spread;
          public NumberCellType2(FpSpread spread, string formatCode)
          {
            _spread = spread;
            NumberFormat = formatCode;
          }
          public string NumberFormat { get; set; }
          public override string Format(object obj)
          {
            GrapeCity.Spreadsheet.NumberFormat numFmt = new GrapeCity.Spreadsheet.NumberFormat(NumberFormat);
            var workbook = _spread.AsWorkbook();
            var formatter = numFmt.GetFormatter(workbook, workbook.WorkbookSet.Culture);
            var result = formatter.Format(obj);
            if (result.Success)
            {
              return result.Text;
            }
            return base.Format(obj);
          }
          public override void PaintCell(Graphics g, Rectangle r, FarPoint.Win.Spread.Appearance appearance, object value, bool isSelected, bool isLocked, float zoomFactor)
          {
            string text = Format(value);
            base.PaintCell(g, r, appearance, text, isSelected, isLocked, zoomFactor);
          }
        }

    You can further use this custom class as shown in the following lines of code:

     NumberCellType numberCellType = new NumberCellType2(fpSpread1, "0.00####");
     numberCellType.DecimalPlaces = 6;
     fpSpread1.ActiveSheet.Cells["A1:A10"].CellType = numberCellType;

    Kindly refer to the updated sample for full implementation. DecimalPointNumberCell_Mod.zip

    Thanks & Regards,

    Aastha

  • Posted 18 September 2023, 5:40 pm EST

    Hello Aastha,

    Thank you very much for your assistance regarding this issue. Your sample project does indeed behave as desired, but when I attempt to bring the code/logic into my main codebase, I keep getting change events from the grid when I don’t think I should be.

    Therefore, I have modified your sample project such that it replicates my set up to the most minimal degree - various grid properties are set, and the cell is populated from an underlying business logic class. This sample now demonstrates my issue.

    Directions:

    1. Load, compile, and run the attached project.
    2. Click into the cell at row 0, column 0.
    3. Click into the textbox at the top of the form - see that a change event is recorded in the logging textbox at the bottom of the form.
    4. Repeat steps 2 and 3, see that change events are continuously firing even though the cell contents were not edited by the user.

    Desired outcome: Clicking in and out of the cell at row 0, column 0 should not cause a change event to fire when the user has not explicitly changed the value.

    Attachment:

    DecimalPointNumberCellModified.zip

    Regards,

    Scott Pearce

  • Posted 19 September 2023, 11:23 pm EST

    Hi Scott,

    Thanks for providing the sample application.

    The FpSpread’s Change event is being fired continuously in your application because you are setting the FpSpread’s cell value inside the DisplayCosts method while handling your data object’s PropertyChanged event. This fires FpSpread’s Change event again and again because a new instance of the value is assigned to the cell every time. For the first time fpspread’s Changed event is fired because the value is assigned to the cell after edit is committed.

    You can modify your DisplayCosts method as shown in the following code snippet to achieve the desired behavior:

    public void DisplayCosts()
    {
        bool oldIsReading = IsReading;
        var val = fpSpread1.ActiveSheet.Cells[0, 0].Value;
        decimal cellValue;
        try
        {
            IsReading = true;
            FarPoint.Win.Spread.CellType.ICellType cellType = fpSpread1.ActiveSheet.GetCellType(0, 0);
            if (val != null && Decimal.TryParse(fpSpread1.ActiveSheet.Cells[0, 0].Value.ToString(), out cellValue))
            {
                // do nothing
            }
            else
            {
                fpSpread1.ActiveSheet.Cells[0, 0].Value = decimal.Parse(((NumberCellType2)cellType).Format(myCosts.LastCost));
            }           
        }
        finally
        {
            IsReading = oldIsReading;
        }
    }

    Kindly refer to the updated sample for implementation. (See Sample_Mod.zip)

    Thanks & Regards,

    Aastha

  • Posted 20 September 2023, 12:25 pm EST

    Hi Aastha,

    Thanks - I can work with this, but is there a way to stop the initial fpSpread Changed event from firing?

    Regards,

    Scott Pearce

  • Posted 22 September 2023, 11:35 pm EST

    Hi Scott,

    The LastCost value that is of decimal type is being converted to double to be stored in the NumericCellType cell after first time edit. Therefore, fpspread’s Changed event is fired for the first time.

    You can consider a workaround to change the data type of the LastCost property to double type and make the required changes in the Form.cs to set the double type value in the cell. We have updated the application for your reference. Please check DecimalPointNumberCell_Mod1.zip

    Thanks & Regards,

    Aastha

Need extra support?

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

Learn More

Forum Channels