Completed
Last Updated: 13 Jun 2018 12:56 by Dimitar
ADMIN
Hristo
Created on: 11 May 2018 08:28
Category: GridView
Type: Bug Report
1
FIX. RadGridView - the GridViewSpreadStreamExport class and its RunExportAsync method may result in incorrect exported cells of a decimal column with a custom ExcelExportType
How to reproduce: 

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();

        this.radGridView1.DataSource = this.GetData();
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;

        GridViewDecimalColumn decimalColumn  = this.radGridView1.Columns[0] as GridViewDecimalColumn;
        decimalColumn.DecimalPlaces = 2;
        decimalColumn.FormatString = "{0:N2}";
        decimalColumn.ExcelExportType = DisplayFormatType.Custom;
        decimalColumn.ExcelExportFormatString = "0.000";
    }

    public object GridViewSpreaExport { get; private set; }

    private object GetData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(double));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Bool", typeof(bool));
        dt.Columns.Add("Date", typeof(DateTime));

        for (int i = 0; i < 100; i++)
        {
            dt.Rows.Add(1.10 + i, "Name " + i, i % 2 == 0, DateTime.Now.AddDays(i));
        }

        return dt;
    }

    private void radButton1_Click(object sender, EventArgs e)
    {
        GridViewSpreadStreamExport spreadExporter = new GridViewSpreadStreamExport(this.radGridView1);
        spreadExporter.FileExportMode = FileExportMode.CreateOrOverrideFile;
        SpreadStreamExportRenderer renderer1 = new SpreadStreamExportRenderer();
        spreadExporter.RunExportAsync(@"..\..\exported-stream.xlsx", renderer1);

    }
}

Workaround: Create a custom SpreadStreamExportRenderer

public class CustomSpreadStreamExportRenderer : SpreadStreamExportRenderer
{
    public override void SetCellValue(DataType dataType, object value)
    {
        switch (dataType)
        {
            case DataType.Number:
                this.SetNumberValue(value);
                break;
            case DataType.DateTime:
                this.SetDateTimeValue(value);
                break;
            case DataType.Boolean:
                this.SetBooleanValue(value);
                break;
            case DataType.Other:
                if (this.SetNumberValue(value))
                {
                    break;
                }
                if (this.SetDateTimeValue(value))
                {
                    break;
                }
                if (this.SetBooleanValue(value))
                {
                    break;
                }

                this.SetStringValue(value);
                break;
            case DataType.String:
            default:
                this.SetStringValue(value);
                break;
        }
    }
}
(Total attached files size should be smaller than 20mb. Allowed extensions: .zip, .rar, .jpg, .png, .gif)
0 comments