C#如何修复DataGridView跨线程操作无效

时间:2016-09-06 21:46:52

标签: c# multithreading datagridview

我想用另一列解密值填充1列,代码可以工作一次,但是当我再次按下按钮时我得到错误"跨线程操作无效"。

    private DataTable LoadData()
    {
        var dt = new DataTable();
        DataSet DS = new DataSet();
        mySqlDataAdapter.Fill(DS);
        dt = DS.Tables[0];
        dataGridView1.DataSource = dt;

        dt.Columns.Add("Key");
        return dt;
    }

    //Thread - > How i can use the LoadData().Rows to not get the cross thread error ?
    public void LoadKeyColumnValues()
    {
        try
        {   //The error is here -> LoadData().Rows
            foreach (DataRow r in LoadData().Rows)
            {
               r["Key"] = Decrypt(r.Field<string>("EncryptedColumn"));
            }
        }
        catch (Exception _ex)
        {
            MessageBox.Show(_ex.ToString());
        }
        finally
        {
            LoadKeyColumn.Abort();
        }

    }

0 个答案:

没有答案
相关问题