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

Export Radgrid to Pdf - Formatting Header/Columns

14 Answers 845 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 19 Feb 2009, 10:56 PM
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

 

14 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 20 Feb 2009, 06:44 AM
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


0
Matt
Top achievements
Rank 1
answered on 23 Feb 2009, 04:15 PM
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.
0
Accepted
Daniel
Telerik team
answered on 23 Feb 2009, 04:34 PM
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.
0
Matt
Top achievements
Rank 1
answered on 23 Feb 2009, 05:47 PM
Thanks for the help guys! Everything working great now.
0
Matt
Top achievements
Rank 1
answered on 23 Feb 2009, 10:30 PM
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? 

 

0
Princy
Top achievements
Rank 2
answered on 24 Feb 2009, 11:00 AM
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.
0
Matt
Top achievements
Rank 1
answered on 26 Feb 2009, 05:07 PM
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

0
Shinu
Top achievements
Rank 2
answered on 27 Feb 2009, 05:01 AM
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
0
Edgar
Top achievements
Rank 1
answered on 17 Jan 2012, 08:10 PM
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.

0
Princy
Top achievements
Rank 2
answered on 18 Jan 2012, 06:54 AM
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.
0
Vivek
Top achievements
Rank 2
answered on 05 Jun 2013, 08:51 AM
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.



0
Pavlina
Telerik team
answered on 07 Jun 2013, 08:24 AM
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.
0
Asutosh
Top achievements
Rank 1
answered on 04 Aug 2014, 07:13 AM
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
0
Shinu
Top achievements
Rank 2
answered on 04 Aug 2014, 07:33 AM
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
Tags
Grid
Asked by
Matt
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Matt
Top achievements
Rank 1
Daniel
Telerik team
Princy
Top achievements
Rank 2
Edgar
Top achievements
Rank 1
Vivek
Top achievements
Rank 2
Pavlina
Telerik team
Asutosh
Top achievements
Rank 1
Share this question
or