Kendo UI: Excel Export not working correctly after datasource refreshing data

kendo-asp.net-mvckendo-gridkendo-ui

I have a Grid, when users click a button, it gets some parameters and refresh datasource:

var grdUP = $("#weblogGrid").data("kendoGrid");
grdUP.dataSource.transport.options.read.url = url; // new url

//Read data source to update
grdUP.dataSource.read();

it works fine. the new data shows in the grid. And the grid has another button, which will export the data to excel. I'm using below code (also tried the built-in button):

var grid = $("#weblogGrid").data("kendoGrid");
grid.saveAsExcel();

it actually exports the data to excel file.

However, it always exports the initial data in the grid, not the data user refreshed.

For example, when the grid first shows up, it has 10 rows data. After refresh, it has 5 rows data. Now, if export, it still exports 10 rows data although the data in grid is different.

Is this a Bug? or, maybe I did something wrong in refresh grid?

Thanks

===============================
edit to clarify something

Thanks. currently, I got new data using:

var url = '/WeblogReport/GetWebLogList?fromDate=' + fromDate + '&toDate=' + toDate;
var grdUP = $("#myGrid").data("kendoGrid");
//Set url property of the grid data source
grdUP.dataSource.transport.options.read.url = url;
//Read data source to update
grdUP.dataSource.read();

So I changed to:

// get value of date
....

$.ajax({
    type: "GET",
    dataType: "json",
    url: "/WeblogReport/GetWebLogList",
    data: { FromDate: fromDate, ToDate: toDate },
    success: function (data) {
        alert(data);

        var grid = $("#myGrid").data("kendoGrid");

        grid.dataSource.data(data);
        grid.refresh();
    }
});

Somehow, it does not show the new data. Any suggestions?

Thank you very much.


add more clarification

Here is in the Json call.

success: function (data) {
    var newdata = [{ "UserName": "username", "ClientIP": "1.1.1.1"}];

    $("#myGrid").data("kendoGrid").dataSource.data(newdata);
    $("#myGrid").data("kendoGrid").refresh();

    //$("#myGrid").data("kendoGrid").saveAsExcel();
}

Best Answer

Set both of the following fields to make Excel export work:

grid.dataSource.transport.options.read.url = url;
grid.options.dataSource.transport.read.url = url;
Related Topic