Export Grid Async and Conditional Formatting

2 Answers 62 Views
GridView
Michael
Top achievements
Rank 2
Iron
Michael asked on 25 Oct 2022, 08:55 PM

When exporting a RadDataGridView with the Export-Visuals enabled this handled what I needed (export conditional formatting), however it slowed the export down greatly. My solution was to swap to Async export. This did make the GUI better, but broke the export of conditional formatting. I jury-rigged a solution that works well enough, although not great. Figured I'd pass this along just in case it helps others


[Main Export Method ...]
BtnExportExcel.Enabled = false;
try
{
    GridViewSpreadExport spreadExporter = new GridViewSpreadExport(this.radGridView1);
    spreadExporter.ExportChildRowsGrouped = true;
    spreadExporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport;
    spreadExporter.HiddenRowOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport;
    spreadExporter.FileExportMode = FileExportMode.CreateOrOverrideFile;
    SpreadExportRenderer exportRenderer = new SpreadExportRenderer();
    spreadExporter.ExportVisualSettings = true;
    lastFileName = fileName;
    using (var bgw = new BackgroundWorker())
    {
        pgb_Saving.Value = 0;
        pnlSaving.Visible = true;
        var bgwFileName = fileName;
        bgw.DoWork += (o,e)=> spreadExporter.RunExport(bgwFileName, exportRenderer);
        bgw.RunWorkerCompleted += SpreadExporter_AsyncExportCompleted;
        bgw.RunWorkerAsync();
    }
                        
}
catch (Exception ex)
{
    BtnExportExcel.Enabled = true;
}
[...]

private void SpreadExporter_AsyncExportCompleted(object sender, AsyncCompletedEventArgs e)
{
    pgb_Saving.Value = 0;
    pnlSaving.Visible = false;
    tsbDataGridViewExportExcel.Enabled = true;
    if (e.Error is null && System.IO.File.Exists(lastFileName))
    {

        string szTemp = "'" + lastFileName + "' created.\r\n\r\nWould you like to open the spreadsheet?";
        var iRet = MsgBox.ConfirmMsg(szTemp, MessageBoxButtons.YesNo, "Spreadsheet Created");
        if (iRet == DialogResult.Yes) System.Diagnostics.Process.Start(lastFileName);
    }
    SwapForExport(false);
}

private void pnlSaving_VisibleChanged(object sender, EventArgs e)
{
    if (pnlSaving.Visible)
    {
        if (pnlSaving.Tag  as Timer is null) {
            var tmr = new Timer() { Interval = 250 };
            tmr.Tick += Timer_Tick;
            pnlSaving.Tag = tmr;
                }
        ((Timer)pnlSaving.Tag).Start();
    }
    else
    {
        if(pnlSaving.Tag is Timer tmr)
        {
            tmr.Stop();
        }
    }
}

private void Timer_Tick(object sender, EventArgs e)
{
//pgb_Saving is a Progress Bar
    pgb_Saving.Value = (pgb_Saving.Value + 1) % pgb_Saving.Maximum;
}

2 Answers, 1 is accepted

Sort by
0
Michael
Top achievements
Rank 2
Iron
answered on 25 Oct 2022, 08:55 PM
See above, this is just to share my solution.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Oct 2022, 08:54 AM

Hello, Michael,     

Indeed, to achieve optimal performance and to decrease the consumed memory, the visual settings and view definitions in RadGridView are not exported when the GridViewSpreadExport is run asynchronously. The GridViewSpreadExport.AsyncExportCompleted event is fired once the export action is completed. Note that you can open the produced file by RadSpreadSprocessing library and customize the worksheet and its cells according to your specific requirements. You can refer to the online documentation of our Document Processing libraries for more information:
https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/overview 
https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/working-with-cells/get-set-clear-properties 

Thank you for sharing your solution with the community. It is not clear what is the exact implementation of the SwapForExport method since it is missing in the provided code snippet. However, I have noticed that in the BackgroundWorker.DoWork event you are running the grid export. Note that the BackgroundWorker always executes its work on a different than the main UI thread.

Note that all UI controls are not thread safe controls in the whole Windows Forms platform (not just Telerik controls, but all controls out there). Here is an article on MSDN, describing how to make thread-safe Winforms UI application. This means that any control from the Telerik UI for WinForms suite is not thread safe as well and cannot be used outside the main UI thread. You should use an Invoke to update the controls in cross threading scenario.

I would highly encourage you to have a look at the following KB article which is quite useful about exporting visual settings with the async export:

https://docs.telerik.com/devtools/winforms/knowledge-base/exporting-radgridview-asynchronously-preserving-its-visual-settings 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Michael
Top achievements
Rank 2
Iron
Answers by
Michael
Top achievements
Rank 2
Iron
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or