CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2007
    Posts
    405

    Cross-thread operation not valid: Control 'gridControl'accessed from a thread other ?

    Cross-thread operation not valid: Control 'gridControl' accessed from a thread other than the thread it was created on.

    Code:
    string sPath = "";
    private void cmdImportExcel1_Click(object sender, EventArgs e)
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "Excel Files (.xls; .xlsx;)|*.xls; *.xlsx";
                dlg.InitialDirectory = Environment.CurrentDirectory;
                dlg.Title = "Bạn chọn file excel hợp đồng...";
                DialogResult dlgresult = dlg.ShowDialog();
    
                if (dlgresult == DialogResult.OK)
                {
                    if (!string.IsNullOrEmpty(dlg.FileName))
                    {
                        sPath = dlg.FileName;
                        Thread myThr = new Thread(ImportExcel1);
                        myThr.Start();//gridView1
                        
                    }
                }
            }
            
            private void ImportExcel1()
            {            
                // Xóa các mẫu tin trong table access
                tmpSQL = "DELETE * FROM HDCNH;";
                if (!clsConnecACS.ThucThiSQL(tmpSQL))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Lỗi xóa table HDCNH không th*nh công !", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
    
                // Lưu các mẫu tin từ Excel v*o Access
                ExcelSaveTableAccess(1, sPath);
    
                // Nhóm các mẫu tin từ Table Access v* lưu v*o gridView1
                tmpSQL = "SELECT CHINHANH, HD, LOAIHANG, TIEUCHUAN, KHACHHANG, Sum(SOLUONG) AS SLUONG, DONGIA, (SLUONG*DONGIA) AS DOANHTHU,NGAYKY, LOAIHD, NGAYGIAO ";
                tmpSQL += "FROM HDCNH ";
                tmpSQL += "GROUP BY CHINHANH, HD, LOAIHANG, TIEUCHUAN, KHACHHANG, DONGIA, NGAYKY, LOAIHD, NGAYGIAO ";
                tmpSQL += "ORDER BY HD;";
                Debug.Print(tmpSQL);
                //Cross-thread operation not valid: Control 'gridControl' accessed from a thread other than the thread it was created on.
                gridControl1.DataSource = clsConnecACS.FillDatatable(tmpSQL);//Error here
                Format_Gridview(gridView1);
            }
    How to fix this error ?

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,402

    Re: Cross-thread operation not valid: Control 'gridControl'accessed from a thread oth

    Quote Originally Posted by dongtrien View Post
    Cross-thread operation not valid: Control 'gridControl' accessed from a thread other than the thread it was created on.
    Yes, direct access to any window created in the main thread form any other thread is not allowed.

    Quote Originally Posted by dongtrien View Post
    How to fix this error ?
    In a multithreaded C++/VC++ application in such a case we usually PostMessage a user defined message to the main GUI thread; the main GUI thread then accesses its child window/control and make the required action (depending on the PostMessage parameters that you have to set).
    Victor Nijegorodov

  3. #3
    Join Date
    Sep 2007
    Posts
    405

    Re: Cross-thread operation not valid: Control 'gridControl'accessed from a thread oth

    I use C# language, I'm looking for Thread example about my case

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured