Search code examples
c#asp.nettelerikexport-to-exceltelerik-grid

How to limit text length in grid column while not limiting the same text in Excel Export


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;
}

Solution

  • 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!