Completed
Last Updated: 09 Oct 2014 07:13 by ADMIN
ADMIN
George
Created on: 03 Sep 2014 10:57
Category: GridView
Type: Bug Report
0
FIX. RadGridView - ExportToExcelML does not call ToString if grid's column is bound to an instance of a class
To reproduce:

Declare the following classes:

 public class Test
 {
     public string Name { get; set; }
     public Child Child { get; set; }

 }

 public class Child
 {
     public string ChildName { get; set; }

     public override string ToString()
     {
         return ChildName;
     }
 }



Add the following columns:

gridViewTextBoxColumn1.FieldName = "Name";
gridViewTextBoxColumn1.HeaderText = "column1";
gridViewTextBoxColumn1.Name = "column1";
gridViewTextBoxColumn2.FieldName = "Child";
gridViewTextBoxColumn2.HeaderText = "column2";
gridViewTextBoxColumn2.Name = "column2";
this.radGridView1.MasterTemplate.Columns.AddRange(new Telerik.WinControls.UI.GridViewDataColumn[] {
gridViewTextBoxColumn1,
gridViewTextBoxColumn2});
this.radGridView1.Name = "radGridView1";
this.radGridView1.Size = new System.Drawing.Size(429, 176);
this.radGridView1.TabIndex = 0;
this.radGridView1.Text = "radGridView1";

Bind RadGridView to the following data:

var data = new List<Test>();

for (int i = 0; i < 10; i++)
{
    data.Add(new Test
        {
            Name = "Name" + i,
            Child = new Child
            {
                ChildName = "Child " + i
            }
        });
}



Export to Excel:

ExportToExcelML excelExporter = new ExportToExcelML(radGridView1);
excelExporter.RunExport(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"test.xls"));

You will notice that column2 will not have text



Workaround:

Subscribe to the ExcelCellFormatting event:

void excelExporter_ExcelCellFormatting(object sender, Telerik.WinControls.UI.Export.ExcelML.ExcelCellFormattingEventArgs e)
{
    if (e.ExcelCellElement.Data.DataItem.GetType().IsClass)
    {
        e.ExcelCellElement.Data.DataItem = e.ExcelCellElement.Data.DataItem.ToString();
    }
}
0 comments