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
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
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.
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?
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.
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
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
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.
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.
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.
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
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