简体   繁体   中英

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. There is a DataGridView in that form which have got 4 columns. I have use a timer to update my DataGridView contents.

But Every time I clear the DataGridView and then I fill it because I have to sort them by date. If I don't use .Rows.Clear() and .Refresh it adds the same content over and over again. 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.

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 . For example, in this code block, it gives in 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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM