1

I have some problem when trying to get value of combobox in IF statement in Backgroundworker. When I try to run this code

 if (KondisiSaldo.SelectedItem == "Kurang dari...")
 {
   view.RowFilter = string.Format("[Saldo] < '{0}'", thresholdcas);
   view2.RowFilter = string.Format("[Saldo] > '{0}'", thresholdcas);


   this.Invoke(new MethodInvoker(delegate
   {
     ViewDataSaldoGV.DataSource = view;
     SaldoUnscheduleGV.DataSource = view2;
   }));
}

The error says

Cross-thread operation not valid: Control 'KondisiSaldo' accessed from a thread other than the thread it was created on.

Can anyone help me ?

Adil
  • 146,340
  • 25
  • 209
  • 204
Alfonsus Dhani
  • 95
  • 2
  • 11
  • 2
    Yes you have to store the value from the form inside a initiated property and then you can call its value from inside the backgroundworker, or pass it in as a parameter. – Edward Jan 31 '17 at 05:27
  • 2
    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) – Martheen Jan 31 '17 at 05:29

1 Answers1

4

You are accessesing KondisiSaldo in non GUI thread. Put the KondisiSaldo in Invoke block to access it on GUI thread as your did with view and view2 controls.

this.Invoke(new MethodInvoker(delegate
{
   if (KondisiSaldo.SelectedItem == "Kurang dari...")
   {
         view.RowFilter = string.Format("[Saldo] < '{0}'", thresholdcas);
         view2.RowFilter = string.Format("[Saldo] > '{0}'", thresholdcas);   

         ViewDataSaldoGV.DataSource = view;
         SaldoUnscheduleGV.DataSource = view2;
   }
}));

You may need to adjust the condition you have.

Adil
  • 146,340
  • 25
  • 209
  • 204