简体   繁体   English

当我修改DataGridView时,它给system.invalidoperationexception跨线程操作无效吗?

[英]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.

相关问题 错误:System.InvalidOperationException:跨线程操作无效 - Error :System.InvalidOperationException: Cross-thread operation not valid 异步任务 - System.InvalidOperationException - 无效的跨线程操作 - async task - System.InvalidOperationException - Invalid cross-thread operation 处理功能区单击事件时,为什么会收到InvalidOperationException(跨线程操作无效)? - Why do I get an InvalidOperationException (cross-thread operation not valid) when processing ribbon click events? 从 Task.Run() 访问所有 ListView 项目会出现此错误 InvalidOperationException: 'Cross-thread operation not valid - access all ListView Items from Task.Run() gives this error InvalidOperationException: 'Cross-thread operation not valid 系统线程:跨线程操作无效 - System Threading: Cross-thread operation not valid .NET BackGroundWorker - InvalidOperationException:跨线程操作无效 - .NET BackGroundWorker - InvalidOperationException : Cross-thread operation not valid 我正在使用backgroundworker并得到:InvalidOperationException:跨线程操作无效-我应该如何处理? - I'm using backgroundworker and getting: InvalidOperationException: Cross-thread operation not valid - how should I handle it? 跨线程操作无效-DataGridView更新数据源 - Cross-thread operation not valid - DataGridView Updating Data Source 从(System.Timer)Timer事件访问进度栏会显示“跨线程操作无效” - Accessing a Progress bar from (System.Timer)Timer Event gives "Cross-Thread Operation Not Valid) 跨线程操作无效 - Cross-thread operation not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM