This is a migrated thread and some comments may be shown as answers.

Paging and Export all records to csv

2 Answers 456 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alejandro
Top achievements
Rank 1
Alejandro asked on 27 Sep 2016, 02:39 PM

Hello,

What Im trying to do, is setting up a radgrid to use paging, and when the user selects the export to csv option it should export all of the records, not just the current page.

 

Ive looked into other posts, but ive tried everything and i just cant get it working. Right now it just exports the current page, when i try other things, the paging stops working and the export of all records works.

 

Here is my aspx:

<%@ Page Title="Nut Runners Data" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="NutRunnersReport.aspx.cs" Inherits="DataServer.Reports.NutRunnersReport" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <link href="../Css/print.css" rel="stylesheet">
    <link href="../Css/radGrid.css" rel="stylesheet">

    <telerik:RadAjaxLoadingPanel ID="LoadingPanel1" runat="server" EnableAjaxSkinRendering="true" Skin="Black" RenderMode="Mobile" AnimationDuration="500">

    </telerik:RadAjaxLoadingPanel>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"  OnAjaxRequest="RadAjaxManager1_AjaxRequest" DefaultLoadingPanelID="LoadingPanel1">

        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="StartDate">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1"/>
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="EndDate">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1"/>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>

    <div class="form-horizontal">
        <div class="row">
            <div class="col-md-12">
                <h3>Nut Runners Data</h3>
                <hr />
                The following nut runners data is between the range of dates selected.
               <br />
                From
                <telerik:RadDatePicker ID="StartDate" runat="server" OnSelectedDateChanged="StartDate_SelectedDateChanged" AutoPostBack="true"></telerik:RadDatePicker>
                To
                <telerik:RadDatePicker ID="EndDate" runat="server" OnSelectedDateChanged="EndDate_SelectedDateChanged" AutoPostBack="true"></telerik:RadDatePicker>
                <br />
            </div>

        </div>
        <div class="row" style="margin-top: 20px;">
            <div class="col-md-12">
                <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" OnAjaxRequest="RadAjaxPanel1_AjaxRequest" LoadingPanelID="LoadingPanel1"  ClientEvents-OnRequestStart="onRequestStart" >
                    <telerik:RadGrid ID="RadGrid1" runat="server" Width="100%" OnGridExporting="RadGrid1_GridExporting" OnItemDataBound="RadGrid1_ItemDataBound" OnItemCommand="RadGrid1_ItemCommand" AllowFilteringByColumn="true"
                                     Skin="Office2010Black" GroupPanelPosition="Top" OnNeedDataSource="RadGrid1_NeedDataSource">
                        <ClientSettings> 
                            <Resizing AllowColumnResize="false" ResizeGridOnColumnResize="false" />
                            <ClientEvents OnCellSelected="" /> 
                            <Selecting AllowRowSelect="true" />
                            <Scrolling AllowScroll="true" UseStaticHeaders="true" SaveScrollPosition="true" ScrollHeight="500"/>
                        </ClientSettings>

                        <MasterTableView AllowAutomaticDeletes="false"
                                         AllowAutomaticInserts="false"
                                         AllowAutomaticUpdates="false"
                                         AllowFilteringByColumn="true"
                                         AllowMultiColumnSorting="true"
                                         AllowPaging="true"
                                         AllowSorting="true"
                                         HorizontalAlign="Center"
                                         ShowHeader="true"
                                         Font-Size="Large"
                                         CommandItemDisplay="Top"                      
                                         Width="100%"
                                         PageSize="20">
                            
                            <PagerStyle Position="Bottom"/>
                            
                            <CommandItemSettings ShowAddNewRecordButton="false" ShowCancelChangesButton="false" ShowExportToCsvButton="true" ShowExportToExcelButton="false" ShowRefreshButton="true" />
                        </MasterTableView>
                    </telerik:RadGrid>
                </telerik:RadAjaxPanel>
                <br />
            </div>
        </div>
    </div>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script src="../Scripts/initialize.js"></script>
        <script src="../Scripts/radChart.js"></script>
        <script src="../Scripts/radGrid.js"></script>
        <script src="../Scripts/radAjaxManager.js"></script>
    </telerik:RadCodeBlock>
</asp:Content>

And here is my c#:
using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataServer.App_Code;
using DataServer.Content.Models;
using Telerik.Web.UI;
using Telerik.Web.UI.GridExcelBuilder;

namespace DataServer.Reports
{
    public partial class NutRunnersReport : System.Web.UI.Page
    {

        public void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            RadDataGridControl.SetPageSizeForExport(sender, e);
        }

        public void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = e.Item as GridDataItem;
                TableCell dateCell = dataItem["Date"];

                dateCell.Text = Convert.ToDateTime(dateCell.Text).ToString("d");
            }
            RadDataGridControl.CenterFilterColumn(RadGrid1);
        }

        ~NutRunnersReport()
        {
        }

        public DateTime? StartDateTime
        {
            get
            {
                return this.StartDate.SelectedDate;
            }
            set
            {
                this.StartDate.SelectedDate = value;
            }
        }

        public DateTime? EndDateTime
        {
            get
            {
                return this.EndDate.SelectedDate;
            }
            set
            {
                this.EndDate.SelectedDate = value;
            }
        }

        public void EndDate_SelectedDateChanged(object sender,
            Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            this.GetProductionResults();
        }

        public void RadAjaxPanel1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            this.GetProductionResults();
        }

        public void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            this.GetProductionResults();
        }

        public void Page_PreRender(object sender, EventArgs e)
        {
            this.EditColumns();

            this.RadGrid1.Rebind();
        }

        public void EditColumns()
        {
            GridColumn[] columns = this.RadGrid1.MasterTableView.AutoGeneratedColumns;
            foreach (GridColumn column in columns)
            {
                switch (column.UniqueName)
                {
                    case "Date":
                        column.HeaderStyle.Width = Unit.Pixel(130);
                        column.FilterControlWidth = Unit.Pixel(90);
                        column.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                        break;
                    case "Shift":
                        column.HeaderStyle.Width = Unit.Pixel(70);
                        column.FilterControlWidth = Unit.Pixel(30);
                        column.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                        break;
                }

                column.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                column.HeaderStyle.VerticalAlign = System.Web.UI.WebControls.VerticalAlign.Middle;
                //column.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                column.ItemStyle.VerticalAlign = System.Web.UI.WebControls.VerticalAlign.Middle;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!this.IsPostBack)
            {
                this.StartDateTime = DateTime.Today;
                this.EndDateTime = DateTime.Today.AddDays(1);
            }

        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.GetProductionResults();
        }

        public void RadGrid1_GridExporting(object source, GridExportingArgs e)
        {
            RadDataGridControl.GridExporting(source, e);
        }

        protected void StartDate_SelectedDateChanged(object sender,
            Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            this.GetProductionResults();
        }



        private void GetProductionResults()
        {
            this.RadGrid1.DataSource = null;
            using (var entities = new NutRunnersEntities())
            {
                this.RadGrid1.DataSource = entities.p_GetNutRunnersByDate(this.StartDateTime,
                                                                                   this.EndDateTime).ToList();
            }
        }
    }
}

The method SetPageSizeForExport:
internal static void SetPageSizeForExport(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
var grid = (Telerik.Web.UI.RadGrid)sender;
            int count = grid.Items.Count;
            if (count == 0)
            {
                count++;
            }

            grid.PageSize = count * grid.MasterTableView.PageCount;
}

And the method GridExporting:
internal static void GridExporting(object source, Telerik.Web.UI.GridExportingArgs e)
{
var grid = (Telerik.Web.UI.RadGrid)source;
grid.ExportSettings.IgnorePaging = true;
grid.ExportSettings.ExportOnlyData = false;
grid.ExportSettings.OpenInNewWindow = true;
grid.ExportSettings.HideStructureColumns = false;
grid.MasterTableView.CommandItemDisplay = Telerik.Web.UI.GridCommandItemDisplay.None;
grid.ExportSettings.FileName = string.Format("{0} Exported Grid Data",
(System.Web.HttpContext.Current.Handler as System.Web.UI.Page).Title);

switch (e.ExportType)
{
case Telerik.Web.UI.ExportType.Excel:
grid.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.Xlsx;
grid.MasterTableView.ExportToExcel();
break;
case Telerik.Web.UI.ExportType.Csv:
grid.MasterTableView.ExportToCSV();
break;
case Telerik.Web.UI.ExportType.Word:
grid.ExportSettings.Word.Format = Telerik.Web.UI.GridWordExportFormat.Html;
grid.MasterTableView.ExportToWord();
break;
case Telerik.Web.UI.ExportType.WordDocx:
grid.ExportSettings.Word.Format = Telerik.Web.UI.GridWordExportFormat.Docx;
grid.MasterTableView.ExportToWord();
break;
case Telerik.Web.UI.ExportType.Pdf:
grid.MasterTableView.ExportToPdf();
break;
}
}


Any suggestion of what im doing wrong? 

Sorry for all the mess.

 

Thanks,

Alex

2 Answers, 1 is accepted

Sort by
0
Accepted
Kostadin
Telerik team
answered on 30 Sep 2016, 10:27 AM
Hi Alejandro,

In order to export all pages you need to set IgnorePging property to true.I examined the provided code and I noticed that you set IgnorePaging on GridExporting event handler but I am afraid this event is fired too late in the page live cycle to apply the property. I would recommend you to set it on ItemCommand even handler when export to CSV command is fired. Please check out the following code snippet.
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if(e.CommandName == RadGrid.ExportToCsvCommandName)
    {
        RadGrid1.ExportSettings.IgnorePaging = true;
    }
}


Regards,
Kostadin
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Alejandro
Top achievements
Rank 1
answered on 30 Sep 2016, 01:47 PM

That worked! Thank you very much for your help!

Regards,

Alejandro

Tags
Grid
Asked by
Alejandro
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Alejandro
Top achievements
Rank 1
Share this question
or