We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Grid ForeignKey export problem

Hello

I have a problem when I try to do an Excell export from a Grid with ForeignKey

I have done my grid, like example from the version 12.4.0.30 (Grid\Foreign Key Column).

@(Html.EJ().Grid<OrdersView>("ForeignKey")

        .Datasource((IEnumerable<object>)ViewBag.dataSource1)

        .

        .

        .ToolbarSettings(toolbar =>

        {

            toolbar.ShowToolbar().ToolbarItems(items =>

            {

                .

                items.AddTool(ToolBarItems.ExcelExport);

                .

            });

        })

        .Columns(col =>

        {

                .

            col.Field("EmployeeID").HeaderText("Employee Name").ForeignKeyField("EmployeeID").ForeignKeyValue("FirstName").DataSource((IEnumerable<object>)ViewBag.dataSource2).TextAlign(TextAlign.Left).Width(90).Add();

         .  

        })

        )

 

I have done my controller export like the example (Grid\Exporting Grid)

 

public void ExportToExcel(string GridModel)

        {

            ExcelExport exp = new ExcelExport();

            GridProperties obj = ConvertGridObject(GridModel);

            exp.Export(obj, ViewBag.dataSource1, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");

        }

 

… but when I run the Export I have an error.

 

How I can do the Excell export from de Grid\Foreign Key Column example?

 

Thanks


3 Replies

BM Balaji Marimuthu Syncfusion Team May 6, 2015 01:33 PM UTC

Hi Juan,

Thanks for using Syncfusion Products.

We have analyzed your provided code snippet and we would like to let you know that if we are using foreign key column in Grid, we need to specify the dataSource to the foreign key column of GridProperties in the export method. Please find the following code snippet:

[View]


@(Html.EJ().Grid<ForeignkeyExport.OrdersView>("FlatGrid")

.Datasource((IEnumerable<object>)ViewBag.dataSource1)

.AllowPaging()

.ToolbarSettings(toolbar =>

{

toolbar.ShowToolbar().ToolbarItems(items =>

{

items.AddTool(ToolBarItems.ExcelExport);

items.AddTool(ToolBarItems.WordExport);

items.AddTool(ToolBarItems.PdfExport);

});

})

.Columns(col =>

{

col.Field("EmployeeID").HeaderText("Employee Name").ForeignKeyField("EmployeeID")

.ForeignKeyValue("FirstName").DataSource((IEnumerable<object>)ViewBag.dataSource2)

.TextAlign(TextAlign.Left).Width(90).Add();


. . . . .


)


[Controller]


public void ExportToExcel(string GridModel)

{

ExcelExport exp = new ExcelExport();

var DataSource = new NorthwindDataContext().OrdersViews.ToList();

GridProperties obj = ConvertGridObject(GridModel);

obj.Columns[2].DataSource = new NorthwindDataContext().EmployeeViews.ToList();

exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");

}

public void ExportToWord(string GridModel)

{

WordExport exp = new WordExport();

var DataSource = new NorthwindDataContext().OrdersViews.ToList();

GridProperties obj = ConvertGridObject(GridModel);

obj.Columns[2].DataSource = new NorthwindDataContext().EmployeeViews.ToList();

exp.Export(obj, DataSource, "Export.docx", false, false, "flat-saffron");

}

public void ExportToPdf(string GridModel)

{

PdfExport exp = new PdfExport();

var DataSource = new NorthwindDataContext().OrdersViews.ToList();

GridProperties obj = ConvertGridObject(GridModel);

obj.Columns[2].DataSource = new NorthwindDataContext().EmployeeViews.ToList();

exp.Export(obj, DataSource, "Export.pdf", false, false, "flat-saffron");

}



For your convenience we have attached a sample in the sample in following link: ForeignkeyExport.zip

If above solution doesn’t meet your requirement, kindly share us your exporting error screen shot that will be helpful to provide better solution.

Please let us know if you have any queries.

Regards,
Balaji Mairmuthu


JP Juan Peiró Llobet May 6, 2015 02:34 PM UTC

 

Hi Balaji.

Thanks for your help. Your solution was perfect.



BM Balaji Marimuthu Syncfusion Team May 7, 2015 12:06 PM UTC

Hi Juan,
Thanks for the update.
Please get back to us if youhave any concerns.
Regards,
Balaji Marimuthu

Loader.
Live Chat Icon For mobile
Up arrow icon