This is a migrated thread and some comments may be shown as answers.

When to set RadGrid.ExportSettings.FileName

5 Answers 1059 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JP
Top achievements
Rank 1
JP asked on 06 Jun 2012, 03:35 PM
Hi,

I want to use a generated filename for exports. I want to generate the filename only when the user does an export (I use the RadGrid's built-in feature for exporting by showing the export to Excel/Pdf/Word buttons).
If I set the filename in CreateChildControls, it works fine.
But in which event can I determine if there is an export running and change the filename? I tried the GridExporting event but it seems too late.

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 07 Jun 2012, 04:55 AM
Hi,

Try setting the FileName in the ItemCommand event as shown below.

C#:
protected void Radgrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.ExportToPdfCommandName)
    {
        Radgrid1.ExportSettings.FileName = "yourfilename";
    }
    if (e.CommandName == RadGrid.ExportToExcelCommandName)
    {
        Radgrid1.ExportSettings.FileName = "yourfilename";
    }
    if (e.CommandName == RadGrid.ExportToWordCommandName)
    {
        Radgrid1.ExportSettings.FileName = "yourfilename";
    }
}

Thanks,
Shinu.
Jeff
Top achievements
Rank 1
commented on 31 Jan 2023, 07:25 PM

Wouldn't you be able to set the file name outside of the if statements, rather than code it 3 times?
Lance | Manager Technical Support
Telerik team
commented on 31 Jan 2023, 07:37 PM

Hi Jeff,

Yes, you can set it anywhere you want, the example from Shinu just omits the file extension. This could have also been written as:

if (e.CommandName == RadGrid.ExportToPdfCommandName)
{
    Radgrid1.ExportSettings.FileName = "yourfilename.pdf";
}
if (e.CommandName == RadGrid.ExportToExcelCommandName)
{
    Radgrid1.ExportSettings.FileName = "yourfilename.xlsx";
}
if (e.CommandName == RadGrid.ExportToWordCommandName)
{
    Radgrid1.ExportSettings.FileName = "yourfilename.docx";
}

However, if we do as you state, it requires more lines of code, but decreases the risk of misspelling the file name

// Extra line 1
var fileName = "";

if (e.CommandName == RadGrid.ExportToPdfCommandName)
{
    fileName = "yourfilename.pdf";
}
if (e.CommandName == RadGrid.ExportToExcelCommandName)
{
    fileName = "yourfilename.xlsx";
}
if (e.CommandName == RadGrid.ExportToWordCommandName)
{
    fileName = "yourfilename.docx";
}

// Extra line 2
Radgrid1.ExportSettings.FileName = fileName;

Ultimately, my suggestion would be to use a switch statement with a base filename

var nameOnly = "filename";

switch(e.CommandName)
{
    case RadGrid.ExportToPdfCommandName:
        Radgrid1.ExportSettings.FileName = nameOnly + ".pdf";
        break;
    case RadGrid.ExportToExcelCommandName:
        Radgrid1.ExportSettings.FileName = nameOnly + ".xlsx";
        break;
    case RadGrid.ExportToWordCommandName:
        Radgrid1.ExportSettings.FileName = nameOnly + ".docx";
        break;
    default:
        break;
}

 

1
Dave
Top achievements
Rank 1
answered on 08 Sep 2015, 06:16 PM
This does not seem to work for me.   I want to add the date and time of the export in the file name but changing the filename at this point does nothing.   Anybody know if it's actually possible?
0
Eyup
Telerik team
answered on 10 Sep 2015, 12:03 PM
Hi Dave,

I've created a sample RadGrid web site to test the described behavior. Could you run the attached application and let me know about the result?

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Dave
Top achievements
Rank 1
answered on 11 Sep 2015, 04:43 PM
Thanks for the reply.  This is pretty much exactly what we tried.  Seeing that this should work pushed me to dig further.      Our grids are created dynamically from code behind and we also use our own export button.      Turns out I can set the export name at the same time our export button is created and this works and the datetime changes each time I click on the button.
0
Pavlina
Telerik team
answered on 14 Sep 2015, 10:10 AM
Hi,

We are glad to hear that the example provided by my colleague Eyup helped to achieve your goal. Do not hesitate to contact us in case other questions arise.

Regards,
Pavlina
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
JP
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Dave
Top achievements
Rank 1
Eyup
Telerik team
Pavlina
Telerik team
Share this question
or