Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
830
Async Export to Excel
posted

I have a code that it's purpose is to export a grid to excel.

The Grid is created in runtime (without a form) and so does the ulraGridExcelExporter...

 

If I create the grid on a different Thread other than the main GUI thread, export it to a file and then double click on the created file in order to open it, then my computer will lock.

I mean, I cannot do "Go to Desktop" or open "My Copmuter" and the disktop itself is not repoponding.

The Excel file will also not open...

The system will be released only after I close the application that tried to export to excel.

 

Here is small code that shows the problem:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinGrid.ExcelExport;

namespace ExcelExportProblem
{
    public partial class Form1 : Form
    {
        UltraGrid grid;
        string filePath = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog ofd = new SaveFileDialog()
            {
                FileName = "",
                Title = "Select Destination Excel File",
                Filter = "Microsoft Excel File (*.xls)|*.xls",
                InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
            };

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                filePath = "";
                return;
            }

            filePath = ofd.FileName;

            MethodInvoker mi = delegate()
            {
                grid = new UltraGrid();
                grid.BindingContext = new BindingContext();

                DataTable dt = new DataTable();
                dt.Columns.Add("Col1");
                dt.Columns.Add("Col2");
                dt.Columns.Add("Col3");

                for (int i = 0; i < 10; i++)
                {
                    dt.Rows.Add(new object[] { 1, 2, 3 });
                }

                grid.SuspendLayout();
                grid.SetDataBinding(dt, "");

                foreach (var col in grid.DisplayLayout.Bands[0].Columns)
                {
                    col.AutoSizeMode = Infragistics.Win.UltraWinGrid.ColumnAutoSizeMode.AllRowsInBand;
                    col.Width = col.CalculateAutoResizeWidth(Infragistics.Win.UltraWinGrid.PerformAutoSizeType.AllRowsInBand, true);
                    col.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect;
                }

                grid.ResumeLayout(true);
            };

            mi.BeginInvoke(null, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog ofd = new SaveFileDialog()
            {
                FileName = "",
                Title = "Select Destination Excel File",
                Filter = "Microsoft Excel File (*.xls)|*.xls",
                InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
            };

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                filePath = "";
                return;
            }

            filePath = ofd.FileName;

            grid = new UltraGrid();
            grid.BindingContext = new BindingContext();

            DataTable dt = new DataTable();
            dt.Columns.Add("Col1");
            dt.Columns.Add("Col2");
            dt.Columns.Add("Col3");

            for (int i = 0; i < 10; i++)
            {
                dt.Rows.Add(new object[] { 1, 2, 3 });
            }

            grid.SuspendLayout();
            grid.SetDataBinding(dt, "");

            foreach (var col in grid.DisplayLayout.Bands[0].Columns)
            {
                col.AutoSizeMode = Infragistics.Win.UltraWinGrid.ColumnAutoSizeMode.AllRowsInBand;
                col.Width = col.CalculateAutoResizeWidth(Infragistics.Win.UltraWinGrid.PerformAutoSizeType.AllRowsInBand, true);
                col.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect;
            }

            grid.ResumeLayout(true);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            UltraGridExcelExporter ultraGridExcelExporter1 = new UltraGridExcelExporter();
            ultraGridExcelExporter1.Export(grid, filePath);
            ultraGridExcelExporter1.Dispose();
        }
    }
}

Am I doing something wrong?

Is it a bug?

Is there a better way to do this? (I tried using InvokeRequired and grid.Invoke whenever I accessed the grid on the code of Button1, but it didn't help).

 

Thanks.

Parents Reply Children