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

Table width when exporting with RadDocument

7 Answers 289 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 01 May 2012, 06:11 AM
Hi,

I've implemented this helpful example code (http://demos.telerik.com/silverlight/#GridView/PrintAndExportWithRadDocument) and it is all working as expected, however the table that is created does not take up the whole width of the page. I notice it uses the column widths of the RadGridView, however they are not ideal in the A4 pdf. I would like the table that is created for the pdf to size itself based on content. Is this possible, and if so, how would I do it? I figure it's got something to do with this line:
cell.PreferredWidth = new TableWidthUnit((float)columns[i].ActualWidth);


Thanks

7 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 01 May 2012, 08:48 AM
Hi Peter,

RadGridView exposes the ElementExporting event. Inside the event args you will find a member to control the width of the exported element.  Once you subscribe to this event inside the EventHandler you can adjust the width of the columns this way.

In case you meet any troubles while implementing this please let me know.


All the best,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Peter
Top achievements
Rank 1
answered on 21 May 2012, 08:22 AM
Hi Pavel,

I'm just getting back to this thread now.
I've looked inside the EventArgs and I see a width property but I'm unsure how to utilise it for my purposes. My PDF button binds similar to the example I followed but I'm also passing in the table as a parameter:
Command="{Binding ExportCommand, Source={StaticResource PrintAndExport}}" CommandParameter="{Binding ElementName=rgvSearchFieldSets}"
I've tried adding the ElementExporting event to my radgridview rgvSearchFieldSets but I'm not sure it's getting raised and even if it were it's a fixed double whereas I'd like the table to size to fit the A4 pdf. I'm not sure how to go about this. Can you offer any further help?
0
Pavel Pavlov
Telerik team
answered on 24 May 2012, 01:12 PM
Hello Peter,

I have reviewed this thread and it seems i have mislead you in my first answer. When using the approach from the demo indeed RadGridview will actually  not utilize its internal export logic and the event would not fire. Please receive my apologies for the wrong answer.

In order to achieve the results you need you should use TableWidhtUnitType Percent  when setting the cell.PreferredWidth. Depending on the percentage value , column widths will be distributed proportionally ( similarly to an HTML table) .

Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Peter
Top achievements
Rank 1
answered on 25 May 2012, 12:08 AM
Thanks for getting back to me Pavel.

I've changed the PrefferedWidth to percentage as you've advised,
cell.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent);
However my program crashes now when it gets to the line,
document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
in the Export method of the PrintAndExport...cs file. Error: "Measure with NaN available size". I don't know what I should do to fix it. Can you advise?

Further on the suggested solution (if I get it to work). By setting a percentage will I then forfeit different widths for each column? For example, one column only contains single numbers, and another long strings, so having them equal width is not ideal.
0
Iva Toteva
Telerik team
answered on 29 May 2012, 06:03 PM
Hi Peter,

There are two options for setting the layout of the table.

The table has a property LayoutMode which determines if it should resize its columns depending on its contents, or the columns' width should be fixed. The default value of the property is "Auto", which means that the document will use an internal algorithm to determine the size of the columns.

The properties that allow control over the width of the columns is the preferred width of the cells. You can set it to a value in pixels or as percent - relatively to the size of the whole table. However, you should always provide a numeric value - either the pixel width or the percent value you wish to set. For example, you can set a column's width to 20 percent using the following code:

cell.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 20);

I hope this helps.

Greetings,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Peter
Top achievements
Rank 1
answered on 30 May 2012, 12:14 AM
I appreciate the response. Setting a fixed preferred percentage width has prevented it from crashing now.

In the example I'm using there a several locations where a PreferredWidth is set (the header cell, the data cell, and an indent cell). I haven't completely got my head around it but I have five columns and I find if I leave the header preferredWidth as it was in pixels but set the PreferredWidth in the AddDataRow method (not the indent) to a percent below 20% it stretches the table but also maintains most of the column widths from the gridView which is what I want. Unfortunately it's still not perfect and it's preventing the class from being as generic (for example I'd get a different appearance if I apply it to a gridview with a different number of columns).

The ideal scenario is for the class to set the widths based on the content of the columns.
0
Iva Toteva
Telerik team
answered on 05 Jun 2012, 10:38 AM
Hi Peter,

Overall, there are two options for creating the table.

You can create a table with fixed layout and setting the PreferredWidth to a value depending on the size of the GridView column. For example, you can calculate a ratio value by dividing the width of the page size by the respective size of the grid view. Afterwards, multiply the width of each column by this ratio factor and use this value as preferred width.

The other option would be to use a table with autofit layout and rely on RadDocument's algorithm to set the widths of the columns depending on their contents.

Please, try these approaches and if you don't manage to accomplish the desired results, attach a demo project in a support ticket, so that we can look into it and provide more specific assistance.

Kind regards,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Peter
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Peter
Top achievements
Rank 1
Iva Toteva
Telerik team
Share this question
or