Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 System.Data.OleDb;
using System.Threading;

namespace SRS
{
    public partial class excelimport : Form
    {
        public excelimport()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfiledialog1 = new OpenFileDialog();

            if (openfiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.textBox1.Text = openfiledialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if ( backgroundWorker1.IsBusy != true)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        DataTable mysource;
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 1; (i <= 10); i++)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                string pathconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + textBox1.Text + " ;Extended Properties="Excel 12.0 Xml;HDR=YES;";";
            OleDbConnection conn = new OleDbConnection(pathconn);

            OleDbDataAdapter mydatapter = new OleDbDataAdapter("select * from [" + textBox2.Text + "$]", conn);
            DataTable dt = new DataTable();

            mydatapter.Fill(dt);
                    datagridview1.datasource=dt;
                    System.Threading.Thread.Sleep(500);
                    worker.ReportProgress((i * 10));
                }
            }


        }


        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.tbprogress.Text = (e.ProgressPercentage.ToString() + "%");
        }

        private delegate void tododelegate();

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((e.Cancelled == true))
            {
                this.tbprogress.Text = "Canceled!";
            }

            else if (!(e.Error == null))
            {
                this.tbprogress.Text = ("Error: " + e.Error.Message);
            }

            else
            {
                this.tbprogress.Text = "Done!";
            }
        }

        private void excelimport_Load(object sender, EventArgs e)
        {
        }


    }
}

I get the error message in line :

datagridview1.datasource=dt;

I tried to alter the code like this :

private DataTable loadingtable()
        {
            string pathconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + textBox1.Text + " ;Extended Properties="Excel 12.0 Xml;HDR=YES;";";
            OleDbConnection conn = new OleDbConnection(pathconn);

            OleDbDataAdapter mydatapter = new OleDbDataAdapter("select * from [" + textBox2.Text + "$]", conn);
            DataTable dt = new DataTable();

            mydatapter.Fill(dt);

            return dt;
        }

and in load event dataGridView1.DataSource = mysource;

and in bw worker mysource =loadingtable();

and then there is no error but then load data takes forever.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
258 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can't update UI controls on a non-UI thread.

Try this:

datagridview1.Invoke(() => datagridview1.datasource = dt);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...