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

Export Detail Grid

Hi,
I am having a little trouble exported a detail grid to excel with the filtered data that is being displayed.

I currently have a master/detail grid set up where the user can select a row from the master grid and then the detail grid is filtered based on what they selected.  My problem is that when I export the filtered detail grid, all of the data from the detail grid is exported and not just the filtered rows.

Here is my row selected event that filters my detail grid:

$(function () {
                        window.rowSelected = function (args) {
                            var value = args;
                            $.ajax({
                                type: "POST",
                                url: "/Reports/LeadsByStateCountyMailDate",
                                success: function (data) {

                                    var state = value.data.State;

                                    var maildate = getFormattedDate(value.data.MailDate);
                           
                                    var datasource = data;
                                    var detaildata = ej.DataManager(data).executeLocal(ej.Query().where(ej.Predicate('State', ej.FilterOperators.equal, state, true).and('MailDate', ej.FilterOperators.equal, maildate, true)));
                                    
                                    var gridObj = $("#DetailGrid").ejGrid("instance");
                                    gridObj.dataSource(ej.DataManager(detaildata));


Here is my controller code:
  public void ExportDetailGrid(string GridModel)
        {
            ExcelExport exp = new ExcelExport();
            var DataSource = LeadsByStateCountyMailDate() ;
            GridProperties obj = ConvertGridObject(GridModel);
            exp.Export(obj, (IEnumerable)DataSource.Data, "LeadsByStateCountyMailDate.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
        }



4 Replies

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team February 16, 2017 10:39 AM UTC

Hi Richard,  
 
We could see you would like to export only the filtered records in the Grid. This can be achieved by using the DataManager and its Adaptor. DataManager supports several Adaptors which are discussed in the following UG. 
 
 
Among these Adaptors, we have achieved your requirement using the UrlAdaptor. In the RowSelected event of the Master Grid, you have to send the POST using the UrlAdaptor with required query. In your code example, you have filtered the records after collecting the data from the server. But in this approach, we are requesting only the data that are needed for the Details Grid by sending the query along with POST. So the result to the client end must be the filtered records.  
 
In the server end, query will be saved to the public variable. The saved variable can be retrieved later while exporting the Grid and so we have filter the records in the server end. Therefore, the Details Grid will be exported with a filtered record alone. Refer to the following code example. 
 
<h3>Master Grid</h3> 
@(Html.EJ().Grid<EmployeeView>("MasterGrid") 
        .Datasource((IEnumerable<object>)ViewBag.datasource1) 
        . .. .  
            . . . . 
        .ClientSideEvents(eve => { eve.RowSelected("rowSelected"); }) 
) 
<h3>Details Grid</h3> 
@(Html.EJ().Grid<OrdersView>("DetailGrid") 
           .  ..  
                  . . . 
) 
 
<script> 
    function rowSelected(args) { 
        var EmpID = args.data["EmployeeID"]; 
 
        //use DataManager with UrlAdaptor 
        var dataManager = ej.DataManager({ 
            url: "/Home/DataSource", 
            adaptor: new ej.UrlAdaptor() 
        }); 
        //Generate Query 
        var query = ej.Query().where(ej.Predicate('EmployeeID', ej.FilterOperators.equal, EmpID, true) 
            .and('OrderDate', ej.FilterOperators.lessThanOrEqual, new Date(), true)); 
 
        var execute = dataManager.executeQuery(query) // executing query 
               .done(function (e) { 
                   var gridObj = $("#DetailGrid").ejGrid("instance"); 
                   //update the Records in done method of the DataManager 
                   gridObj.dataSource(ej.DataManager(ej.parseJSON(e.result))); 
               }); 
    } 
 
</script> 
 
    public class HomeController : Controller 
    { 
        public static DataManager query = new DataManager(); 
 
        public ActionResult DataSource(DataManager dm) { 
            //save the query for exporing purpose 
            query = dm; 
            IEnumerable data = OrderRepository.GetAllRecords().ToList(); 
            DataOperations dp = new DataOperations(); 
            //filter the records based on the DataManager dm query and return 
            //So you no need to filter in the client end 
            data = dp.PerformWhereFilter(data, dm.Where, dm.Where[0].Condition); 
            return Json(data, JsonRequestBehavior.AllowGet); 
        } 
        public void ExportToExcel(string GridModel) 
        { 
            ExcelExport exp = new ExcelExport(); 
            IEnumerable DataSource = OrderRepository.GetAllRecords().ToList(); 
            DataOperations dp = new DataOperations(); 
            //use the public query to filter the data 
            DataSource = dp.PerformWhereFilter(DataSource, query.Where, query.Where[0].Condition); 
            GridProperties obj = ConvertGridObject(GridModel); 
            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron"); 
        } 
 
    } 
 
Using the DataOperations class from Syncfusion Library and query, the records will be filtered. We have already discussed about the server-side APIs in the following KB. 
 
 
We have also prepared a sample that can be downloaded from the following location. 
 
 
Regards, 
Seeni Sakthi Kumar S.  



RD Richard Dublon February 17, 2017 08:09 PM UTC

That worked perfectly...thanks


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team February 20, 2017 04:16 AM UTC

Hi Richard, 
 
Thanks for the update. We are happy to hear that your requirement is achieved. Please get back to us, if you require further assistance on thi. 
 
Regards, 
Seeni Sakthi Kumar S. 



SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team February 20, 2017 04:17 AM UTC

Hi Richard, 
 
Thanks for the update. We are happy to hear that your requirement is achieved. Please get back to us, if you require further assistance on thi. 
 
Regards, 
Seeni Sakthi Kumar S. 


Loader.
Up arrow icon