RadGrid - Export to CSV unable to set custom headers

3 posts, 1 answers
  1. Roelf
    Roelf avatar
    10 posts
    Member since:
    Mar 2015

    Posted 25 Oct Link to this post

    Hi

    We are using a 2 language system and set the headers of the RadGrid according to the user's selected language and works fine for display. The RadGrid should be able to export to Excel and CSV. The excel exports the headers correctly, but the CSV only uses default column names set in the ASPX file. We are using version 2015.2.729. Here is an abbreviated version of our code:

    <telerik:RadGrid ID="grdBlocked" runat="server" AutoGenerateColumns="False" OnItemDataBound="grdBlocked_ItemDataBound" OnNeedDataSource="grdBlocked_NeedDataSource" OnInfrastructureExporting="grdBlocked_InfrastructureExporting" OnGridExporting="grdBlocked_GridExporting" OnItemCommand="grdBlocked_ItemCommand">
                        <ExportSettings ExportOnlyData="True" IgnorePaging="true">
                            <Excel Format="Xlsx" FileExtension=".xlsx" />
                        </ExportSettings>
                        <MasterTableView DataKeyNames="StudentNumber, DateBlocked" CommandItemDisplay="Top">
                            <CommandItemSettings ExportToPdfText="Export to PDF" ShowAddNewRecordButton="False" ShowExportToCsvButton="True" ShowExportToExcelButton="True" ShowExportToPdfButton="False" ShowExportToWordButton="False" ShowRefreshButton="False"></CommandItemSettings>
                            <Columns>
                                <telerik:GridBoundColumn DataField="StudentNumber" HeaderText="Student number" UniqueName="StudentNumber"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Name" HeaderText="Student Name" UniqueName="Name"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="DateBlocked" HeaderText="Date" UniqueName="Date" DataType="System.DateTime" DataFormatString="{0:yyyy/MM/dd HH:mm}"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="BlockedUser" HeaderText="User" UniqueName="User"></telerik:GridBoundColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>

     

    Code behind (read comments):

    using System;
    using System.Web.UI;
    using Telerik.Web.UI;
     
    namespace WritingCentreOnlineBookings.Reports
    {
        public partial class BlockedAccounts : System.Web.UI.Page
        {
            private Includes.Site masterPage = new Includes.Site();
            private WebAPICalls MyWebAPI = new WebAPICalls();
     
            protected void BindData()
            {
                grdBlocked.DataSource = MyWebAPI.GetDataTable("GetBlockedStudents");
                grdBlocked.DataBind();
            }
     
            protected void grdBlocked_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
            {
                if (e.Item.ItemType == GridItemType.Header)
                {
                    GridHeaderItem header = (GridHeaderItem)e.Item;
                    header["StudentNumber"].Text = (masterPage.LanguageCode == "afr" ? "Studentenommer" : "Student number");
                    header["Name"].Text = (masterPage.LanguageCode == "afr" ? "Naam van student" : "Student name");
                    header["Date"].Text = (masterPage.LanguageCode == "afr" ? "Datum" : "Date");
                    header["User"].Text = (masterPage.LanguageCode == "afr" ? "Geblok deur" : "Blocked by");
                }
            }
     
            protected void Page_Load(object sender, EventArgs e)
            {
                masterPage = (Includes.Site)Master;
     
                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }
     
            protected void grdBlocked_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                grdBlocked.DataSource = MyWebAPI.GetDataTable("GetBlockedStudents");
            }
     
            //Tried to change the column headers here, but it never gets called for CSV, only for excel
            protected void grdBlocked_InfrastructureExporting(object sender, GridInfrastructureExportingEventArgs e)
            {
                for (int i = 2; i <= e.ExportStructure.Tables[0].Rows.Count; i++)
                {
                    var cell = e.ExportStructure.Tables[0].Cells[1, i];
                    //Forces the student number to be text in Excel
                    cell.Format = "@";
                    cell = e.ExportStructure.Tables[0].Cells[3, i];
                    cell.Value = ((DateTime)cell.Value).ToString("yyyy-MM-dd HH:mm");
                }
            }
     
            //Documentation suggests this is the place to add customization. It gets called, but does not seem to have an effect
            protected void grdBlocked_GridExporting(object sender, GridExportingArgs e)
            {
                GridItem[] headerItems = grdBlocked.MasterTableView.GetItems(GridItemType.Header);
                headerItems[0].Cells[2].Text = (masterPage.LanguageCode == "afr" ? "Studentenommer" : "Student number");
            }
     
            //Also tried to rebind, just to make sure the column headers are loaded
            protected void grdBlocked_ItemCommand(object sender, GridCommandEventArgs e)
            {
                if (e.CommandName == RadGrid.ExportToCsvCommandName)
                {
                    grdBlocked.Rebind();
                }
            }
        }
    }
  2. Answer
    Kostadin
    Admin
    Kostadin avatar
    1711 posts

    Posted 28 Oct Link to this post

    Hello Roelf,

    A possible solution is to change the column names on PreRender event handler and call rebind. Please check out the following code snippet.
    protected void grdBlocked_PreRender(object sender, EventArgs e)
    {
        grdBlocked.MasterTableView.GetColumn("StudentNumber").HeaderText = "Studentenommer";
        grdBlocked.MasterTableView.GetColumn("Name").HeaderText = "Naam van student";
        grdBlocked.MasterTableView.GetColumn("Date").HeaderText = "Datum";
        grdBlocked.MasterTableView.GetColumn("User").HeaderText = "Geblok deur";
        grdBlocked.Rebind();
    }

    You can use a boolean variable to distinguish whether the export command is fired if you do not want to execute this logic on each postback.

    Regards,
    Kostadin
    Telerik by Progress
    Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Roelf
    Roelf avatar
    10 posts
    Member since:
    Mar 2015

    Posted 04 Nov in reply to Kostadin Link to this post

    This is a workable solution. Thank you
Back to Top