0

I want to populate 1 column with another column decrypted values, the code works once, but when i press the button again i get the error "Cross-thread operation not valid".

    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();
        }

    }
X11
  • 332
  • 2
  • 15
  • 1
    Possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) and also: http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c?rq=1 – Ňɏssa Pøngjǣrdenlarp Sep 06 '16 at 21:47
  • You are apparently loading from a non-UI-thread. How is the `LoadKeyColumnValues` method called? – Maarten Sep 07 '16 at 09:04
  • Button click -> Load some data to datagridview -> start LoadKeyColumn Thread – X11 Sep 07 '16 at 09:21

0 Answers0