Hello,
I'm looking for help on exporting data from a RadGrid to a PDF file, via the ExportToPDF control button. I've read a handful of threads on this, but none are working. I'm trying to get the cell alignment to be correct for text (left-aligned) and numeric values (right-aligned). Part of my difficulty is the fact the visible columns is dynamic, so I don't have a unique name to look for. This also causes a possible issue with too many columns to fit onto the page without manually formatting the output.
Because I cannot effect the client's experience (the UI layout), I've moved my code from the 'ItemCommand' event to the 'ItemCreated' event. This allows me to get all the column to fit on the page, but I'm still having issues with setting the text cell alignment left and the numeric alignment right. I'm able to set them all to either: left, right, center, none. Another issue I have, is that by doing this in the ItemCreated event, there's no data to determine what type of text is going to be in the cell.
I'm looking for help on exporting data from a RadGrid to a PDF file, via the ExportToPDF control button. I've read a handful of threads on this, but none are working. I'm trying to get the cell alignment to be correct for text (left-aligned) and numeric values (right-aligned). Part of my difficulty is the fact the visible columns is dynamic, so I don't have a unique name to look for. This also causes a possible issue with too many columns to fit onto the page without manually formatting the output.
Because I cannot effect the client's experience (the UI layout), I've moved my code from the 'ItemCommand' event to the 'ItemCreated' event. This allows me to get all the column to fit on the page, but I'm still having issues with setting the text cell alignment left and the numeric alignment right. I'm able to set them all to either: left, right, center, none. Another issue I have, is that by doing this in the ItemCreated event, there's no data to determine what type of text is going to be in the cell.
protected
void
RadGrid_AssetList_ItemCreated(
object
sender, GridItemEventArgs e )
{
if
(IsExportButtonClicked){
FormatPdfOutput(RadGrid_AssetList);
var item = e.Item
as
GridDataItem;
if
(item !=
null
){
var dataItem = item;
foreach
(TableCell cell
in
dataItem.Cells){
cell.Style[
"text-align"
] =
"center"
;
}
}
else
{
var gridHeaderItem = e.Item
as
GridHeaderItem;
if
(gridHeaderItem !=
null
){
var headerItem = gridHeaderItem;
foreach
(TableCell cell
in
headerItem.Cells){
cell.Style[
"text-align"
] =
"center"
;
}
}
else
{
var gridFooterItem = e.Item
as
GridFooterItem;
if
(gridFooterItem !=
null
){
var footerItem = gridFooterItem;
foreach
(TableCell cell
in
footerItem.Cells){
cell.Style[
"text-align"
] =
"center"
;
}
}
}
}
}
}
private
void
FormatPdfOutput(RadGrid radGrid){
radGrid.ExportSettings.IgnorePaging =
true
;
radGrid.ExportSettings.ExportOnlyData =
true
;
radGrid.ExportSettings.HideStructureColumns =
true
;
double
marginWidth = radGrid.ExportSettings.Pdf.PageLeftMargin.Value
+ radGrid.ExportSettings.Pdf.PageRightMargin.Value;
double
printArea = radGrid.ExportSettings.Pdf.PageWidth.Value - marginWidth;
//if the print area of the page is smaller than the RadGrid width (total column width), change to
//percent based widths and evenly break up the columns.
if
(printArea < radGrid.Width.Value){
//count the number of columns that are both visible and have header text.
var visibleColumnCount = radGrid.Columns.Cast<GridColumn>().Count(column => column.Visible);
//calculate the percentage to evenly display all the columns
var tempWidth = 1.0 / (
double
)visibleColumnCount;
foreach
(GridColumn column
in
radGrid.Columns){
if
(column.Visible){
column.Visible = column.HeaderText.Length > 0;
}
column.HeaderStyle.Width = column.Visible
? Unit.Percentage(tempWidth)
: Unit.Percentage(0);
column.HeaderStyle.Wrap =
true
;
if
(column.Visible){
column.HeaderStyle.HorizontalAlign = column
as
GridNumericColumn ==
null
? HorizontalAlign.Left
: HorizontalAlign.Right;
}
}
}
}