Copying excel sheet data from folder and dispalying in datagridview in c# selenium

Rakesh kumar 106 Reputation points
2022-03-29T11:40:16.237+00:00

I am copying excel sheet data from folder and displaying in datagridview without using database and getting error like this way
"System.InvalidOperationException: 'Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on.'"

Please find below the code

using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Threading;

namespace Image_Retrival
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

        var t = new Thread((ThreadStart)(() => {   
         
            using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel workbook|*.xlsx", Multiselect = false })  
            {  



                if (ofd.ShowDialog() == DialogResult.OK)  
                {  
                    Cursor.Current = Cursors.WaitCursor;  
                    DataTable dt = new DataTable();  
                    using (XLWorkbook workbook = new XLWorkbook(ofd.FileName))  
                    {  
                        bool isfirstrow = true;  
                        var rows = workbook.Worksheet(1).RowsUsed();  
                        foreach (var row in rows)  
                        {  
                            if (isfirstrow)  
                            {  
                                foreach (IXLCell cell in row.Cells())  
                                    dt.Columns.Add(cell.Value.ToString());  
                                isfirstrow = false;  
                            }  

                            else  
                            {  
                                dt.Rows.Add();  
                                int i = 0;  
                                foreach (IXLCell cell in row.Cells())  
                                    dt.Rows[dt.Rows.Count - 1][i++] = cell.Value.ToString();  
                            }  
                              
                        }  

                   
                        dataGridView1.DataSource = dt.DefaultView;  
                         
                        Cursor.Current = Cursors.Default;  
                    }  
                }  
            }  
             }));  
        t.SetApartmentState(ApartmentState.STA);  
        t.Start();  
        t.Join();  
    }  

187759-image.png

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,835 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,398 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
323 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,125 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2022-03-30T02:28:24.12+00:00

    As an intermediate approach, try replacing the problematic line with:

    BeginInvoke( new Action( ( ) => dataGridView1.DataSource = dt.DefaultView ) );

    1 person found this answer helpful.
    0 comments No comments