ReoGrid Forum

Fast and powerful .NET Spreadsheet Component

You are not logged in.

Announcement

This forum has been archived and no longer accepts new user registrations. Please report your questions, problems, and feedback to the issue page of ReoGrid on GitHub. Thank you for your cooperation.

https://github.com/unvell/ReoGrid/issues

#1 2015-07-14 08:35:13

ebrouen
Member
Registered: 2015-05-20
Posts: 6

Custom Data Formatter and GetData

Hi Jing,

In this version, the IDataFormatter is working. Thanks.
In the documentation that you provided, you use GetData<double> to get the value as a number and to convert it. It works the first time (value set by code) but if the user change the value, the internal Data member of the cell returns to a string and GetData<double>() returns 0 (even if the user types only figures).
I had to convert it manually to a numeric value.
If the setting of Edit_AutoFormatCell is false, then it doesn't work even the first time because the Custom DataFormatter isn't called at all.

        private void Form1_Load(object sender, EventArgs e)
        {
            Worksheet ws = grid.Worksheets[0];
            unvell.ReoGrid.DataFormat.DataFormatterManager.Instance.DataFormatters.Add(unvell.ReoGrid.DataFormat.CellDataFormatFlag.Custom, new MyDataFormatter());
            
            // This, if uncommented, disables the Custom format
            //ws.SetSettings(unvell.ReoGrid.WorksheetSettings.Edit_AutoFormatCell, false);
            ReoGridCell cell = ws.Cells[1, 1];

            cell.DataFormat = unvell.ReoGrid.DataFormat.CellDataFormatFlag.Custom;
            cell.DataFormatArgs = "SecondToTime";
            cell.Data = 1200;

            cell = ws.CreateAndGetCell(1, 2);
            cell.DataFormat = unvell.ReoGrid.DataFormat.CellDataFormatFlag.Custom;
            cell.DataFormatArgs = "SecondToTime";


        }
    }

    class MyDataFormatter : unvell.ReoGrid.DataFormat.IDataFormatter
    {
        public string FormatCell(ReoGridCell cell)
        {
            // This doesn't work
            double val = cell.GetData<double>();

            // This works
            double val2 = 0;
            unvell.ReoGrid.Utility.CellUtility.TryGetNumberData(cell.Data, out val2);
            
            // This works also (without test for incorrect entry)
            double val1 = double.Parse(cell.Data.ToString());
            
            var df = cell.DataFormatArgs;
            if (df != null)
            {
                if (df is string)
                {
                    switch (df as string)
                    {
                        case "SecondToTime":
                            {
                                // doesn't handle the incorrect values
                                return new TimeSpan(0, 0, (int)val1).ToString();
                                break;
                            }
                    }
                }
            }
            return val1 < 0 ? string.Format("[{0}]", (-val1).ToString("###,###,##0.00")) : val1.ToString("###,###,###.00");
        }

        public bool PerformTestFormat()
        {
            return true;
        }
    }

Fortunately, there is a workaround smile

Best regards
Eric

Offline

#2 2015-07-14 15:22:24

Jingwood
Moderator
From: jing at reogrid.net
Registered: 2014-06-03
Posts: 615

Re: Custom Data Formatter and GetData

Hi Eric,

Thanks for the information, and very nice sample.

The Cell.GetData<double> method would be fixed in next release.

Regards, Jing

Offline

#3 2015-08-03 06:00:51

Jingwood
Moderator
From: jing at reogrid.net
Registered: 2014-06-03
Posts: 615

Re: Custom Data Formatter and GetData

This problem has been fixed in 0.8.9.2. Thanks for report.

Offline

Board footer

Powered by FluxBB