RadGrid - Exporting to CSV does not include template column
So I've got the following column defined in my radgrid:
<telerik:GridTemplateColumn DataField="Payment.Customer.FirstName" DataType="System.String"
HeaderText="First Name" SortExpression="Payment.Customer.FirstName" UniqueName="FirstName">
<ItemTemplate>
<asp:HyperLink ID="hypFirstName" runat="server" Target="_blank" Text='<%# ((PaymentIssue)Container.DataItem).Payment.Customer.FirstName %>'
NavigateUrl='<%# string.Format("~/CustomerAdmin/Customer_View.aspx?customerId={0}", ((PaymentIssue)Container.Dat开发者_运维百科aItem).Payment.CustomerId) %>'></asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
Now, if I use the RadGrid MasterTableView.ExportToExcel() function; all is well. However; if I try MasterTableView.ExportToCSV(), the column is blank.
Any ideas on what could be causing this?
OK; I figured this out. Turns out I need to set the Text property on the GridDataItems before exporting.
Sample code below:
Export Buttons Click Method
protected void lnkExport_Click(object sender, EventArgs e)
{
var linkButton = (LinkButton)sender;
switch (linkButton.CommandArgument)
{
case "Excel":
rgPaymentIssues.MasterTableView.ExportToExcel();
break;
case "CSV":
PrepareRadGridForExport();
rgPaymentIssues.MasterTableView.ExportToCSV();
break;
default:
break;
}
}
PrepareRadGridForExport snippet
foreach (GridDataItem gi in rgPaymentIssues.MasterTableView.Items)
{
var hypFirstName = (HyperLink) gi.FindControl("hypFirstName");
gi["FirstName"].Text = hypFirstName.Text;
}
Setting ExportOnlyData to 'False' solves this issue. See the following link for the details.
http://www.telerik.com/help/aspnet-ajax/grid-csv-export.html
Just in case someone arrives here as I did, you could solve it another way, put another column with Readonly="true" so it doesn't appear in editing mode and the original column with Display="false" so it doesn't appear in view mode, then Export is all good, here's the extract:
<telerik:GridBoundColumn DataField="Email2" HeaderText="Email" UniqueName="Email2a" ReadOnly="true" Exportable="true" />
<telerik:GridTemplateColumn DataField="Email2" HeaderText="Email" UniqueName="Email2" Display="false" Exportable="false">
<EditItemTemplate>
...
</EditItemTemplate>
</telerik:GridTemplateColumn>
Happy coding!
精彩评论