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

Dynamic Column Widths in RadGrid PDF Export?

4 Answers 316 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 10 Jul 2013, 11:46 PM
We use Radgrids throughout our application - with each page tied to a base class that handles OnPdfExporting for all grids.

After a lot of fussing I have sort of figured out how to format the grid.  (working code snippet below).  The problem is that this one routine handles many dozen different grids - and it appears to make every column the exact same width in the PDF.  Is there any way (code example would be great) to dynamically size the PDF columns based on their content - without having to handle each specific grid individually?

Protected Overloads Sub grid_pdfExport(ByVal source As Object, ByVal e As GridPdfExportingArgs)
 
    e.RawHTML = e.RawHTML.Replace("<br>", "<br/>")
    e.RawHTML = Regex.Replace(e.RawHTML, "</?(div).*?>", "")
    e.RawHTML = Regex.Replace(e.RawHTML, "</?(a).*?>", "")
 
    e.RawHTML = e.RawHTML.Replace("width: 100%; table-layout: auto;", "width:100% ; border: 1px solid #666; border-collapse: collapse; margin: 0;")
    e.RawHTML = e.RawHTML.Replace("<th scope=""col"">", "<th scope=""col"" style=""font-weight: normal; color: #fff; background-color: #333; border: 1px solid #f1f1f1; white-space: normal; padding: 2px 3px; vertical-align: bottom; line-height: 18px; text-align: left; font-family: arial; font-size: 8px; "">")
    e.RawHTML = e.RawHTML.Replace("<td>", "<td style=""font-weight: normal; color: #000; background-color: #fff; border: 1px solid #f1f1f1; white-space: normal;border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; border-right: 1px solid #f1f1f1; border-left: 1px solid #f1f1f1; padding: 2px 5px; vertical-align: top; text-align: left; font-family: arial; font-size: 8px; "">")
 
End Sub

4 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 15 Jul 2013, 02:09 PM
Hello Dan,

A possible solution is to set the widths of the column dynamically on PagePreRender event handler. Check out the following code snippet.
protected void Page_PreRender(object o, EventArgs e)
{
    GridColumn column = RadGrid1.MasterTableView.GetColumn("MyColumnName");
    column.HeaderStyle.Width = Unit.Pixel(300);
}

Regards,
Kostadin
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
Dan
Top achievements
Rank 1
answered on 29 Jul 2013, 05:12 PM
Thank you for the reply.  Your suggestion would require revisiting dozens of screens and writing custom handling for each column.  I am hoping for find a way to be a bit more dynamic, as the routine to handle the export is in one place for all pages.
0
Daniel
Telerik team
answered on 01 Aug 2013, 11:39 AM
Hi Dan,

I'm afraid there is no built-in functionality for resizing columns based on their content. It would be hard to implement this except if you use mono-spaced font but even in this case you will have to perform a lot of calculations, not to say that the fixed-width fonts are looking outdated.
Starting from the Q2 2013 SP1 release of the RadControls for ASP.NET AJAX we introduced a forced text-wrapping so you can set any width to your columns. The overflowing text will be carried over the next line automatically. More information is available in the blog post here.
Telerik’s Export to PDF Functionality for ASP.NET AJAX Now Greatly Improved

Regards,
Daniel
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
Dan
Top achievements
Rank 1
answered on 06 Aug 2013, 11:02 PM
Thanks for the link.  The Text wrapping is a nice step in the right direction.

Bummed that there is not a solution solve the auto-column sizing, but put my vote in for that on the DEV list.
Tags
Grid
Asked by
Dan
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Dan
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or