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

Radgrid export to pdf problem

0 Answers 90 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chuck
Top achievements
Rank 1
Chuck asked on 13 Jul 2012, 06:25 PM
Hi, I am pretty new to using the Telerik controls and am trying to clean up some pre-existing code. It seems pretty straightforward as I am just exporting a grid to pdf. The issue I am having is that I can load the page, not resize any columns, click the export to pdf button and my grid formats and looks fantastic. if i resize ANY of the columns though, not all my columns show up. This even happens if i resize and make my columns smaller. Help?

<telerik:RadGrid ID="grdEmployee" runat="server" AllowPaging="True"
    AllowSorting="True" GroupingEnabled="False" AllowMultiRowSelection="True"
    GridLines="None" AutoGenerateColumns="False" PageSize ="20"
    AllowCustomPaging="True" Height="400px" CellSpacing="0" 
    OnPageIndexChanged="grdEmployee_PageIndexChanged"
    OnPageSizeChanged="grdEmployee_PageSizeChanged"
    OnItemCommand="grdEmployee_ItemCommand"
    OnItemCreated="grdEmployee_ItemCreated"
    OnPreRender="grdEmployee_PreRender"
    OnSortCommand="grdEmployee_SortCommand"
    OnExportCellFormatting="grdEmployee_ExportCellFormatting"
    meta:resourcekey="grdEmployee">
    <ClientSettings AllowColumnHide="True" AllowColumnsReorder="True" ReorderColumnsOnClient="true">
        <Selecting AllowRowSelect="True" />
        <Scrolling AllowScroll="True" EnableVirtualScrollPaging="True" UseStaticHeaders="True" />
        <Resizing AllowColumnResize="True" AllowResizeToFit="True" />
    </ClientSettings>
    <MasterTableView AllowNaturalSort="False" DataKeyNames="EmployeeId" EnableHeaderContextMenu="False">
        <NestedViewTemplate>
            <asp:Panel ID="pnlEmployeeDetails" runat="server" CssClass="GridDetailPanel">
                <table>
                    <tr>
                        <td class="gdpLabelColumn">
                            <asp:Label ID="lblDetailFirstNameText" runat="server" meta:resourceKey="lblDetailFirstNameText" />
                        </td>
                        <td class="gdpValueColumn" colspan="3">
                            <asp:Label ID="lblDetailFirstNameValue" runat="server" />
                              
                            <asp:Label ID="lblDetailMiddleInitialValue" runat="server" />
                              
                            <asp:Label ID="lblDetailLastNameValue" runat="server" />
                        </td>
                        <td rowspan="5">
                              
                            <!--photo place holder-->
                        </td>
                    </tr>
                    <tr>
                        <td class="gdpLabelColumn">
                            <asp:Label ID="lblDetailBirthDateText" runat="server" meta:resourceKey="lblDetailBirthDateText" />
                        </td>
                        <td class="gdpValueColumn">
                            <asp:Label ID="lblDetailBirthDateValue" runat="server" />
                        </td>
                        <td class="gdpLabelColumn">
                            <asp:Label ID="lblDetailGenderText" runat="server" meta:resourceKey="lblDetailGenderText" />
                        </td>
                        <td class="gdpValueColumn">
                            <asp:Label ID="lblDetailGenderValue" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="gdpLabelColumn">
                            <asp:Label ID="lblDetailHireDateText" runat="server" meta:resourceKey="lblDetailHireDateText" />
                        </td>
                        <td class="gdpValueColumn">
                            <asp:Label ID="lblDetailHireDateDetailValue" runat="server"  />
                        </td>
                        <td class="gdpLabelColumn">
                            <asp:Label ID="lblDetailTerminationDateText" runat="server" meta:resourceKey="lblDetailTerminationDateText"  />
                        </td>
                        <td class="gdpValueColumn">
                            <asp:Label ID="lblDetailTerminationDateValue" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="gdpLabelColumn">
                            <asp:Label ID="lblDetailSiteText" runat="server"
                                meta:resourceKey="lblDetailSiteText" />
                        </td>
                        <td class="gdpValueColumn">
                            <asp:Label ID="lblDetailSiteValue" runat="server" />
                        </td>
                        <td class="gdpLabelColumn">
                            <asp:Label ID="lblDetailDepartmentText" runat="server" meta:resourceKey="lblDetailDepartmentText" />
                        </td>
                        <td class="gdpValueColumn">
                            <asp:Label ID="lblDetailDepartmentValue" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="gdpLabelColumn">
                            <asp:Label ID="lblDetailClassificationText" runat="server" meta:resourceKey="lblDetailClassificationText" />
                        </td>
                        <td class="gdpValueColumn">
                            <asp:Label ID="lblDetailClassificationValue" runat="server"  />
                        </td>
                        <td class="gdpLabelColumn">
                            <asp:Label ID="lblDetailEmployeeGroupText" runat="server" meta:resourceKey="lblDetailEmployeeGroupText" />
                        </td>
                        <td class="gdpValueColumn">
                            <asp:Label ID="lblDetailEmployeeGroupValue" runat="server" />
                        </td>
                    </tr>
                </table>
            </asp:Panel>
        </NestedViewTemplate>
        <CommandItemSettings ExportToPdfText="Export to PDF" ShowAddNewRecordButton="False" ShowRefreshButton="false" />
        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" />
        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True" />
        <Columns>
            <telerik:GridClientSelectColumn UniqueName="Select">
                <HeaderStyle HorizontalAlign="Left" Width="20px" />
            </telerik:GridClientSelectColumn>
            <telerik:GridImageColumn UniqueName="CombinedEmployeeStatus"
                DataAlternateTextField="CombinedEmployeeStatus"
                DataImageUrlFields="EnabledStatus,ActiveStatus"
                DataImageUrlFormatString="~/Style/Images/Grid/Employee {0} {1}.bmp"
                ImageAlign="Middle"
                ImageHeight="16px" ImageWidth="16px">
                <HeaderStyle Width="20px" />
            </telerik:GridImageColumn>
            <telerik:GridTemplateColumn UniqueName="FirstName" SortExpression="FirstName"
                meta:resourceKey="grdEmployee_FirstName">
                <ItemTemplate>
                    <asp:HyperLink ID="hlkSelect" runat="server" ForeColor="#3B3B3B"
                        Text='<%# DataBinder.Eval(Container, "DataItem.FirstName") %>'
                        meta:resourceKey="hlkSelect" />
                </ItemTemplate>
                <HeaderStyle Width="15%" />
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn UniqueName="MiddleInitial" DataField="MiddleInitial"
                SortExpression="MiddleInitial" meta:resourceKey="grdEmployee_MiddleInitial">
                <HeaderStyle Width="35px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="LastName" DataField="LastName"
                SortExpression="LastName" meta:resourceKey="grdEmployee_LastName">
                <HeaderStyle Width="25%" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="EmployeeNumber" DataField="EmployeeNumber"
                SortExpression="EmployeeNumber" meta:resourceKey="grdEmployee_EmployeeNumber">
                <HeaderStyle Width="15%" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="SSN" DataField="SSN"
                SortExpression="SSN" meta:resourceKey="grdEmployee_SSN" />
            <telerik:GridBoundColumn UniqueName="BirthDate" Display="False" DataField="BirthDate"
                SortExpression="BirthDate" meta:resourceKey="grdEmployee_BirthDate" />
        </Columns>
        <SortExpressions>
            <telerik:GridSortExpression FieldName="LastName" />
        </SortExpressions>
    </MasterTableView>
    <PagerStyle Mode="Slider" />
    <FilterMenu EnableImageSprites="False" />
    <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default" />
</telerik:RadGrid>

#region Grid Events
 
/// <summary>
/// Handles the ItemCreated event of the grdEmployee control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.Web.UI.GridItemEventArgs"/> instance containing the event data.</param>
protected void grdEmployee_ItemCreated(object sender, GridItemEventArgs e)
{
    //set the grid column properties
    if ((e.Item is GridDataItem) && (_gridDataExport == false))
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        string employeeId = dataItem.GetDataKeyValue(_GRID_DATAKEY).ToString();
        //Check the selected item, if any on the current page
        MarkSelectedItems(dataItem, employeeId);
        //Add the link to navigate to employee record
        HyperLink selectLink = (HyperLink)dataItem.FindControl("hlkSelect");
        selectLink.NavigateUrl = AptaFundUrl.ToEmployeeEdit() + "&EmployeeId=" + employeeId;
 
    }
    else if ((e.Item is GridDataItem) && (_gridDataExport == true) && (_gridDataPdfExport == true))//pdf export
    {
        //pdf export column formatting
        e.Item.Style["text-align"] = "left";
    }
}
 
/// <summary>
/// Handles the SortCommand event of the grdEmployee control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.Web.UI.GridSortCommandEventArgs"/> instance containing the event data.</param>
protected void grdEmployee_SortCommand(object sender, GridSortCommandEventArgs e)
{
    try
    {
        //cancel the current sort
        e.Canceled = true;
        GridSortExpression sortExpression = new GridSortExpression();
        //get the new sort field name
        sortExpression.FieldName = e.SortExpression;
        //set the new sort order ascending/descending
        sortExpression.SortOrder = e.NewSortOrder;
        //store the sort expression, which can be used when they come back from another  screen
        StoreSortExpressionToSession(sortExpression);
        //make sure only selected items are stored.(the user might have changed the selections
        this.StoreGridSelectedItems(this.grdEmployee);
        //bind the gird with new sort
        this.DoSearch(false, sortExpression);
    }
    catch (Exception ex)
    {
        base.CatchPageErrorAndLog(ex);
    }
}
 
/// <summary>
/// Handles the ItemCommand event of the grdEmployee control. Fired when the grid is expanded
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.Web.UI.GridCommandEventArgs"/> instance containing the event data.</param>
protected void grdEmployee_ItemCommand(object sender, GridCommandEventArgs e)
{
    try
    {
        if (e.CommandName == RadGrid.ExpandCollapseCommandName && !e.Item.Expanded)
        {
            //show the details of the employee
            PopulateEmployeeDetailTemplate(e.Item as GridDataItem, e);
        }
    }
    catch (Exception ex)
    {
        base.CatchPageErrorAndLog(ex);
    }
}
 
/// <summary>
/// Handles the ExportCellFormatting event of the grdEmployee control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.Web.UI.ExportCellFormattingEventArgs"/> instance containing the event data.</param>
protected void grdEmployee_ExportCellFormatting(object sender, ExportCellFormattingEventArgs e)
{
    if (_gridDataExcelExport == true) //do this only for excel export
    {
        //header item formatting
        GridHeaderItem HeaderItem = (GridHeaderItem)grdEmployee.MasterTableView.GetItems(GridItemType.Header)[0];
        foreach (TableCell cell in HeaderItem.Cells)
        {
            cell.Style["text-align"] = "left";
        }
        // each column data formatting
        e.Cell.Style["text-align"] = "left";
    }
}
 
/// <summary>
/// Handles the PreRender event of the grdEmployee control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void grdEmployee_PreRender(object sender, EventArgs e)
{
    //We are using our own select all record option
    //We want to allow the user to uncheck rows after they do a select all first
    //See below why we want do that
    //The Telerik grid top header checkbox by default, only selects records listed on the page
    //Also, the Telerik grid will check the top checkbox, if all the rows displayed in the page is checked(we may not want this, as user might have deselected some rows)
    if (_gridDataExport == false) //do this only when not exporting data
    {
        foreach (GridHeaderItem headerItem in grdEmployee.MasterTableView.GetItems(GridItemType.Header))
        {
            //Find the SelectAll CheckBox in the header using the ClientSelectColumn's  "UniqueName" 
            if (headerItem["Select"].Controls[0] != null)
            {
                CheckBox chkSelectAll = headerItem["Select"].Controls[0] as CheckBox;
 
                if (chkSelectAll != null)
                {
                    //hide it
                    chkSelectAll.Visible = false;
                }
            }
        }
    }
    else if ((_gridDataExport == true) && (_gridDataPdfExport == true)) //pdf export
    {
        //format header columns for pdf export
        foreach (GridHeaderItem headerItem in grdEmployee.MasterTableView.GetItems(GridItemType.Header))
        {
            headerItem["FirstName"].Style["text-align"] = "left";
            headerItem["LastName"].Style["text-align"] = "left";
            headerItem["MiddleInitial"].Style["text-align"] = "left";
            headerItem["EmployeeNumber"].Style["text-align"] = "left";
            headerItem["SSN"].Style["text-align"] = "left";
        }
        GridColumn column = grdEmployee.MasterTableView.GetColumn("FirstName");
        column.HeaderStyle.Width = Unit.Pixel(50);
        GridColumn column2 = grdEmployee.MasterTableView.GetColumn("MiddleInitial");
        column2.HeaderStyle.Width = Unit.Pixel(50);
    }
}
 
/// <summary>
/// Handles the PageSizeChanged event of the grdEmployee control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.Web.UI.GridPageSizeChangedEventArgs"/> instance containing the event data.</param>
protected void grdEmployee_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
    try
    {
        if (_gridDataExport == false)
        {
            StoreGridSelectedItems(this.grdEmployee);
            DoSearch(false);
        }
    }
    catch (Exception ex) //capture any error here to show it as we use AJAX
    {
        base.CatchPageErrorAndLog(ex);
    }
}
 
/// <summary>
/// Handles the PageIndexChanged event of the grdEmployee control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.Web.UI.GridPageChangedEventArgs"/> instance containing the event data.</param>
protected void grdEmployee_PageIndexChanged(object sender, GridPageChangedEventArgs e)
{
 
    try
    {
        StoreGridSelectedItems(this.grdEmployee);
        DoSearch(false);
    }
    catch (Exception ex)  //capture any error here to show it as we use AJAX
    {
        base.CatchPageErrorAndLog(ex);
    }
}
 
/// <summary>
/// Handles the Click event of the btnGridExportToExcel control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnGridExportToExcel_Click(object sender, EventArgs e)
{
   
    _gridDataExport = true; //set this so that other grid events can check for this and not execute it as we do not need that when exporting
    _gridDataExcelExport = true;
 
    //Following is necessary for getting all rows from  database.
    DoSearch(false);
 
    grdEmployee.MasterTableView.RowIndicatorColumn.Visible = false;
 
    //hide the select checkbox column
    grdEmployee.MasterTableView.Columns.FindByUniqueName("Select").Visible = false;
    grdEmployee.MasterTableView.Columns.FindByUniqueName("CombinedEmployeeStatus").Visible = false;
 
    grdEmployee.ExportSettings.HideStructureColumns = true;
    grdEmployee.ExportSettings.OpenInNewWindow = true;
    grdEmployee.ExportSettings.ExportOnlyData = true;
    grdEmployee.ExportSettings.IgnorePaging = true;
    grdEmployee.MasterTableView.ExportToExcel();
}
 
/// <summary>
/// Handles the Click event of the btnGridExportToPDF control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnGridExportToPDF_Click(object sender, EventArgs e)
{
    _gridDataExport = true; //set this so that other gird events can check for this and not execute it as we do not need that when exporting
    _gridDataPdfExport = true;
     
    DoSearch(false);
    //hide the select checkbox column
    grdEmployee.MasterTableView.Columns.FindByUniqueName("Select").Visible = false;
    grdEmployee.MasterTableView.Columns.FindByUniqueName("CombinedEmployeeStatus").Visible = false;
 
    //format columns for fitting within page in portrait mode
    grdEmployee.MasterTableView.Columns.FindByUniqueName("SSN").HeaderStyle.Width = Unit.Pixel(100);
    grdEmployee.MasterTableView.Columns.FindByUniqueName("SSN").ItemStyle.Width = Unit.Pixel(100);
    grdEmployee.MasterTableView.Columns.FindByUniqueName("EmployeeNumber").HeaderStyle.Width = Unit.Pixel(100);
    grdEmployee.MasterTableView.Columns.FindByUniqueName("EmployeeNumber").ItemStyle.Width = Unit.Pixel(100);
    grdEmployee.MasterTableView.Columns.FindByUniqueName("FirstName").ItemStyle.Width = Unit.Pixel(100);
    grdEmployee.MasterTableView.Columns.FindByUniqueName("FirstName").HeaderStyle.Width = Unit.Pixel(100);
    grdEmployee.MasterTableView.Columns.FindByUniqueName("LastName").HeaderStyle.Width = Unit.Pixel(100);
    grdEmployee.MasterTableView.Columns.FindByUniqueName("LastName").ItemStyle.Width = Unit.Pixel(100);
    grdEmployee.MasterTableView.Columns.FindByUniqueName("MiddleInitial").ItemStyle.Width = Unit.Pixel(100);
    grdEmployee.MasterTableView.Columns.FindByUniqueName("MiddleInitial").HeaderStyle.Width = Unit.Pixel(100);
 
    grdEmployee.ExportSettings.OpenInNewWindow = true;
    grdEmployee.ExportSettings.ExportOnlyData = true;
    grdEmployee.ExportSettings.IgnorePaging = true;
    grdEmployee.ExportSettings.Pdf.PageLeftMargin = Unit.Pixel(0);
    grdEmployee.ExportSettings.Pdf.PageRightMargin = Unit.Pixel(0);
     
    grdEmployee.MasterTableView.ExportToPdf();
}
 
#endregion

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Chuck
Top achievements
Rank 1
Share this question
or