Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
public void LoadData()
       {
           var manager = new ManageFactory<ISmartDealerManager>().GetManager();
           List<DealerItem> DealerCollection = manager.LoadDealerData();
           MainDataGrid.Items.Clear();
           MainDataGrid.ItemsSource = null;
           Thread.Sleep(2000);
           if (DealerCollection.Count() > 0)
           {
               MainDataGrid.Items.Clear();
               this.Dispatcher.BeginInvoke(new Action(() =>
               {
                   MainDataGrid.ItemsSource = DealerCollection;
                   lblError.Visibility = Visibility.Hidden;
               }), DispatcherPriority.Background);

           }
           else
           {
               MainDataGrid.ItemsSource = null;
               lblError.Visibility = Visibility.Visible;
           }
           EnableDisableItems();
           this.WaitingScreen.IsLoadingVisible = true;
       }
   }

Thread MyNewThread = new Thread(new ThreadStart(() => LoadData()));
            MyNewThread.IsBackground = true;
            MyNewThread.Start();




Here i am getting error at line " MainDataGrid.Items.Clear();"

and idea please.
Posted
Updated 17-Apr-14 22:00pm
v3
Comments
[no name] 18-Apr-14 9:39am    
Idea for what? The error message is telling you exactly what the problem is.

1 solution

Hi Raman,

The answer is very simple, you are trying to clear the MainDataGrid.Items outside of the UI thread.

You should change your code:

C#
if (DealerCollection.Count() >= 0)
{
  
    this.Dispatcher.BeginInvoke(new Action(() =>
    {
        MainDataGrid.Items.Clear();
        MainDataGrid.ItemsSource = DealerCollection;
        lblError.Visibility = Visibility.Hidden;
    }), DispatcherPriority.Background);

}</pre>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900