-
March 12th, 2023, 10:06 PM
#1
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 ?
-
March 13th, 2023, 11:14 AM
#2
Re: Cross-thread operation not valid: Control 'gridControl'accessed from a thread oth
 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.
 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).
Victor Nijegorodov
-
March 13th, 2023, 08:47 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|