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 ?
Re: Cross-thread operation not valid: Control 'gridControl'accessed from a thread oth
Quote:
Originally Posted by
dongtrien
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
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).
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