Reputation: 255
I'm currently building an asp.net application with a large amount of data in a Telerik RadGrid. A number of the columns in my grid contain large amounts of text. To limit the size of the text in those columns (and to preserve the format of the grid) I truncate each field in the ItemDataBound(...) method in the following piece of code:
if (item["Title"].Text.Length > 21)
item["Title"].Text = item["Title"].Text.Substring(0, 21);
if (item["Investigation_Results"].Text.Length > 30)
item["Investigation_Results"].Text = item["Investigation_Results"].Text.Substring(0, 30);
if (item["Resolution_Steps"].Text.Length > 30)
item["Resolution_Steps"].Text = item["Resolution_Steps"].Text.Substring(0, 30);
The problem is that when using the Excel export functionality of the RadGrid, the fields that are being truncated are also truncated in the exported csv.
Q: How can I have the data truncated in the grid view, but in full when exporting to Excel?
Additional Info:
Export is called on this button click event:
protected void imgbtnexcel_Click(object sender, ImageClickEventArgs e)
{
ConfigureExport();
RadGridActionItem.MasterTableView.ExportToExcel();
}
protected void ConfigureExport()
{
RadGridActionItem.ExportSettings.ExportOnlyData = true;
RadGridActionItem.ExportSettings.IgnorePaging = true;
RadGridActionItem.ExportSettings.OpenInNewWindow = true;
}
Upvotes: 1
Views: 1574
Reputation: 13965
Can you set CSS styles in the RadGrid? Would it be acceptable to have all the text there, but have it truncated by means of CSS?
In a normal table
, you could do the following:
Set this style on the table itself:
table { table-layout: fixed; width: 100%; }
On the actual cell, you'd set up a style ("title" in the example below):
td.title { white-space:nowrap; overflow: hidden; width: 200px; }
The data would still be there, just truncated.
Upvotes: 1
Reputation: 255
I was able to get the functionality I desired by putting a conditional statement (exporting/not exporting) around my "truncating code" in ItemDataBound(...). I then used a global boolean variable and marked it true in the export button click event.
static bool exportBool;
In ItemDataBound(...):
if (ActionItem.exportBool != true) // MK CHANGE
{
if (item["Title"].Text.Length > 21)
item["Title"].Text = item["Title"].Text.Substring(0, 21) + "...";
if (item["Investigation_Results"].Text.Length > 30)
item["Investigation_Results"].Text = item["Investigation_Results"].Text.Substring(0, 30) + "...";
if (item["Resolution_Steps"].Text.Length > 30)
item["Resolution_Steps"].Text = item["Resolution_Steps"].Text.Substring(0, 30) + "...";
}
Export Button Click Event:
protected void imgbtnexcel_Click(object sender, ImageClickEventArgs e)
{
ActionItem.exportBool = true;
BindGrid();
ConfigureExport();
RadGridActionItem.MasterTableView.ExportToExcel();
}
Note that the boolean is set to false in Page_Load().
Thanks!
Upvotes: 1
Reputation: 525
One way to do this is to handle the NeedDataSource event. In that event, supply a datasource with either full or truncated text based on whether you're exporting. You haven't provided this code, but you should be able to tell if you're exporting using the GridExporting event, a button click event, or something similar.
Upvotes: 2
Reputation: 4803
The radgrid's excel export generates a sheet based on what's data bound to the grid.
If you want to full text to display in the export but not on screen, you'll need to hide the text with css and/or javascript.
Upvotes: 1