Master Master - 9 months ago 63
C# Question

ReportViewer exported event callback

I'm using ReportViewer and I'm looking to listen to the result of the exports. I would like to get a callback on the export details (filename and filepath). I googled around and was only able to find the reportviewer.export event but that's an event that is triggered before the export. I'm looking for something afterwards. Any tips would be greatly appreciated.

Answer Source

You could implement your own ReportExport event in order to add functionality after the export.

This is an example in VB.NET:

Private Sub ReportViewer1_ReportExport(sender As Object, e As Microsoft.Reporting.WinForms.ReportExportEventArgs) Handles ReportViewer1.ReportExport

    e.Cancel = True 'stop the default ReportExport event

    Dim strExtension As String = ""

    Select Case UCase(e.Extension.Name)

        Case "PDF"
            strExtension = ".pdf"

        Case "EXCEL"
            strExtension = ".xls"

        Case "WORD"
            strExtension = ".doc"

    End Select

    Dim dlgSaveFile As New System.Windows.Forms.SaveFileDialog

    dlgSaveFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    dlgSaveFile.Filter = e.Extension.LocalizedName + " (*" + strExtension + ")|*" + strExtension + "|All files(*.*)|*.*"

    If dlgSaveFile.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

        Me.ReportViewer1.ExportDialog(e.Extension, e.DeviceInfo, dlgSaveFile.FileName)

        'add here your functionality, for example a simple MsgBox that display where file is saved
        MsgBox("File saved in: " & dlgSaveFile.FileName)

    End If

End Sub