[英]When I modify DataGridView, It gives system.invalidoperationexception cross-thread operation not valid?
I have a database which is SQLite and I have a windows form application. 我有一个SQLite数据库,并且有一个Windows窗体应用程序。 There is a DataGridView
in that form which have got 4 columns. 这种形式的DataGridView
有4列。 I have use a timer to update my DataGridView
contents. 我使用计时器来更新我的DataGridView
内容。
But Every time I clear the DataGridView
and then I fill it because I have to sort them by date. 但是每次我清除DataGridView
然后填充它,因为我必须按日期对它们进行排序。 If I don't use .Rows.Clear()
and .Refresh
it adds the same content over and over again. 如果我不使用.Rows.Clear()
和.Refresh
它将.Rows.Clear()
添加相同的内容。 So I use them. 所以我用它们。
I use different way like comment lines, but problem is 我使用不同的方式,例如注释行,但问题是
system.invalidoperationexception cross-thread operation not valid
I have tried all solution of StackOverflow and whole internet, but all the way gives my same exception. 我已经尝试了所有StackOverflow和整个Internet的解决方案,但是所有方法都给了我同样的例外。
What is my problem. 我怎么了 I have called this method directly and I have called it as a thread, It gave me same exception. 我直接调用了此方法,并将其作为线程调用,它给了我同样的异常。 It gives me exception when I use DataGridView
. 当我使用DataGridView
时,它给了我例外。 For example, in this code block, it gives in dgwIslemGemisi.DataSource=dt;
例如,在此代码块中,它给出dgwIslemGemisi.DataSource=dt;
private void guncellemeIslemGecmisiGoster()
{
dt = db.TumGuncellemeIslemGecmisiGetir();
dgwIslemGecmisi.DataSource = dt;
dgwIslemGecmisi.Update();
dgwIslemGecmisi.Rows.Clear();
dgwIslemGecmisi.Refresh();
////Set AutoGenerateColumns False
//dgwIslemGecmisi.AutoGenerateColumns = false;
////Set Columns Count
//dgwIslemGecmisi.ColumnCount = 4;
////Add Columns
//dgwIslemGecmisi.Columns[0].Name = "islemAdi";
//dgwIslemGecmisi.Columns[0].HeaderText = "İşlem Adı";
//dgwIslemGecmisi.Columns[0].DataPropertyName = "islemAdi";
//dgwIslemGecmisi.Columns[1].HeaderText = "İşleme Başlangıç Tarihi";
//dgwIslemGecmisi.Columns[1].Name = "islemBaslangicTarihi";
//dgwIslemGecmisi.Columns[1].DataPropertyName = "islemBaslangicTarihi";
//dgwIslemGecmisi.Columns[2].Name = "islemBitisTarihi";
//dgwIslemGecmisi.Columns[2].HeaderText = "İşlemin Tamamlanma Tarihi";
//dgwIslemGecmisi.Columns[2].DataPropertyName = "isleminBitisTarihi";
//dgwIslemGecmisi.Columns[3].Name = "islemDurumu";
//dgwIslemGecmisi.Columns[3].HeaderText = "İşlem Durumu";
//dgwIslemGecmisi.Columns[3].DataPropertyName = "islemDurumu";
//dgwIslemGecmisi.DataSource = dt;
//for (int i = 0; i < dt.Rows.Count; i++)
//{
// string durum = "Başarısız";
// if (dt.Rows[i]["islemDurumu"].ToString() == "1")
// {
// durum = "Başarılı";
// }
// dgwIslemGecmisi.Rows.Add(new object[] {
// dt.Rows[i]["islemAdi"].ToString(),
// dt.Rows[i]["islemBaslangicTarihi"].ToString(),
// dt.Rows[i]["islemBitisTarihi"].ToString(),
// durum
// });
//}
dt.Dispose();
}
This is solution of exception. 这是例外的解决方案。
private void guncellemeIslemGecmisiGoster()
{
dt = db.TumGuncellemeIslemGecmisiGetir();
dgwIslemGecmisi.Invoke(new Action(() => dgwIslemGecmisi.DataSource = dt));
dgwIslemGecmisi.Invoke(new Action(() => dgwIslemGecmisi.Update()));
dt.Dispose();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.