Export Radgrid to Pdf - Formatting Header/Columns

15 posts, 2 answers
  1. Matt
    Matt avatar
    10 posts
    Member since:
    Nov 2008

    Posted 19 Feb 2009 Link to this post

    Hello,
    I am trying to Export the data from a radgrid to a pdf.  I have created the CommandItemTemplate below for what I would like to display in the header:

    <

    CommandItemTemplate>

     

     

        <div style="text-align:left; padding-bottom:8px; float:left;">

     

     

            <asp:Label ID="lblCompanyDetailReport" runat="server" Text="Company Detail Report" Visible="false" Font-Italic="true" Font-        Names="Verdana" Font-Size="10px"></asp:Label><br />

     

     

            <asp:Label ID="lblFilterSetting" runat="server" Visible="false" Font-Italic="true" Font-Names="Verdana" Font-         Size="8px"></asp:Label>

     

     

            <asp:Label ID="lblDateRange" runat="server" Visible="false" Font-Italic="true" Font-Names="Verdana" Font-        Size="8px"></asp:Label>

     

     

        </div>

     

     

    </CommandItemTemplate>

    When I export my data, it puts the CommandItemTemplate data above the first radgrid column only.  I would like it to extend as far as it needs.  Is this possible?

    Also, the exported RadGrid columns are fixed widths (i.e. the total space is divided equally among all columns).  Is it possible to specify widths for each column? 

    I appreciate any help you can provide!

    Matt

     

  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Feb 2009 Link to this post

    Hi Matt,

    You can set desired width for the column in the click event of Export button as shown below.

    CS:
     protected void Button1_Click(object sender, EventArgs e) 
        { 
     
            RadGrid1.MasterTableView.GetColumn("SupplierID").HeaderStyle.Width = Unit.Pixel(500); 
            RadGrid1.ExportSettings.ExportOnlyData = false
            RadGrid1.MasterTableView.ExportToPdf(); 
        } 

    Thanks
    Shinu


  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Matt
    Matt avatar
    10 posts
    Member since:
    Nov 2008

    Posted 23 Feb 2009 Link to this post

    Thanks Shinu.  That helps with my formatting of the grid itself, however, i'd still like to stretch the header text across the entire grid, preferably using the equivalent of a colspan.  Do you have any idea of how to do this? 

    I appreciate your help.
  5. Answer
    Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 23 Feb 2009 Link to this post

    Hello Matt,

    To attain this functionality you can use the CommandItemTemplate together with the following code:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
      if (e.Item is GridCommandItem) 
        e.Item.PrepareItemStyle(); 

    Hope this helps.

    Best regards,
    Daniel
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Matt
    Matt avatar
    10 posts
    Member since:
    Nov 2008

    Posted 23 Feb 2009 Link to this post

    Thanks for the help guys! Everything working great now.
  7. Matt
    Matt avatar
    10 posts
    Member since:
    Nov 2008

    Posted 23 Feb 2009 Link to this post

    One more thing regarding Exporting a RadGrid to Pdf...

    Is it possible to right-align the content of certain columns?  I have tried using:

    UserDetailGrid.MasterTableView.Columns.FindByUniqueName(

    "Hour").ItemStyle.HorizontalAlign = HorizontalAlign.Right;

    But it doesn't work. 

    Any ideas? 

     

  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Feb 2009 Link to this post

    Hello Matt,

    Try implementing the following code and see if it makes any difference:
    cs:
     
    bool isExport = false;   
    protected void ExportButton_Click(object sender, EventArgs e)   
        {   
            isExport = true;   
            UserDetailGrid.MasterTableView.ExportToPdf();          
        }   
       
    protected void UserDetailGrid_PreRender(object sender, EventArgs e)   
        {   
            foreach (GridDataItem dataItem in UserDetailGrid.MasterTableView.Items)   
            {   
                if (isExport)   
                {   
                    dataItem["Hour"].Style["background-color"] = "yellow";                
                    dataItem["Hour"].Style["text-align"] = "right";   
                }   
            }   
        }   

    Thanks
    Princy.
  9. Matt
    Matt avatar
    10 posts
    Member since:
    Nov 2008

    Posted 26 Feb 2009 Link to this post

    Thank you Princy!

    How would I use the PreRender Method to right-align the Column Headers?

    When I try to use:

     

    foreach(GridHeaderItem headerItem in CompanyDetailGrid.MasterTableView.Items)

     

     

        if (isPdfExport)

     

        {

            dataItem[

    "Hour"].Style["text-align"] = "right";

     

        }

    I get this error:
    Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'Telerik.Web.UI.GridHeaderItem'.

    I've been looking at several instances of similar errors and cannot find anything that solves it!

    As always, any  input is greatly appreciated.

    Dan

  10. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Feb 2009 Link to this post

    Hi Matt,

    Try aligning the  column headers as shown below.

    CS:
     foreach (GridHeaderItem headerItem in CompanyDetailGrid.MasterTableView.GetItems(GridItemType.Header)) 
            { 
                if (isPdfExport) 
                { 
                    headerItem ["Hour"].Style["text-align"] = "right"
                } 
               
            } 


    Thanks
    Shinu
  11. Edgar
    Edgar avatar
    5 posts
    Member since:
    Jul 2010

    Posted 17 Jan 2012 Link to this post

    I try that and is not working.

    protected

     

     

    void ActivitiesGrid_PreRender(object sender, EventArgs e)

     

    {

     

     

    foreach (GridHeaderItem header in ActivitiesGrid.MasterTableView.GetItems(GridItemType.Header))

     

    {

     

     

    if (isPdfExport)

     

    {

    header.HorizontalAlign =

     

    HorizontalAlign.Left;

     

    header.ForeColor = System.Drawing.

     

    Color.White;

     

    }

    }

    }

    then when I export to pdf the headers are center horizontal-aligned and black colored.

  12. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Jan 2012 Link to this post

    Hello,

    Try the following code.
    C#:
    bool isPdfExport = false;
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
      if (isPdfExport && e.Item is GridHeaderItem)
       {
         GridHeaderItem headerItem = (GridHeaderItem)e.Item;
         headerItem.Style["text-align"] = left;
         headerItem.ForeColor = System.Drawing.Color.White;
       }
    }

    Thanks,
    Princy.
  13. Vivek
    Vivek avatar
    38 posts
    Member since:
    Mar 2013

    Posted 05 Jun 2013 Link to this post

    I'm also facing the same issue where I'm setting width using below piece of code - 

    e.RawHTML = e.RawHTML.Replace("<col  />", "<col style=\"width:90px;\" />");  

    This works great but the problem is - this sets the equal width for all the columns but I want different width to be set in first column.

    To achive this, I tried the setting width using UniqueName but this also didn't work, first column data is still wraped to 90px width.

                RadGrid2.MasterTableView.GetColumn("InvName").HeaderStyle.Width = Unit.Pixel(150);
                RadGrid2.MasterTableView.GetColumn("InvName").ItemStyle.Width = Unit.Pixel(200);

    Please let me know if you have any solution to set different width for the first column.



  14. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 07 Jun 2013 Link to this post

    Hello Vivek,

    Note that you should use only HeaderStyle-Width for setting column width. Remove ItemStyle-Width property from your code and see if it works as expected.

    Regards,
    Pavlina
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  15. Asutosh
    Asutosh avatar
    130 posts
    Member since:
    Sep 2013

    Posted 04 Aug 2014 in reply to Shinu Link to this post

    hi shinu 
    i am below code to export my rad grid to pdf
    but header column is not align
    how can i do that?
    i have attached image also
  16. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 04 Aug 2014 in reply to Asutosh Link to this post

    Hi Asutosh,

    Please take a look at the following help documentation to set style on PDF Export. Provide your code if this doesn't help.
    PDF Export

    Thanks,
    Shinu
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017