title: Update DataGrid in WPF from another window
body: <p>I have a DataGrid control on a window called MainWindow. From MainWindow i open another window called NewCustomerWindow using the following code on a button click.</p>
  
  <pre><code>NewCustomerWindow newCustomerWindow = new NewCustomerWindow();
  newCustomerWindow.ShowDialog();
  </code></pre>
  
  <p>In the NewCustomerWindow i have a form with textboxes where someone can enter information about a customer and then these information are saved into a database. After i save these information i want to update the DataGrid in the MainWindow so i use the following code</p>
  
  <pre><code>var mainWindow = Application.Current.Windows
                  .OfType&lt;MainWindow&gt;()
                  .FirstOrDefault(window =&gt; window is MainWindow);
  
  mainWindow.customersGrid.Items.Add(new 
              {
                  CustomerID = txtCustomerID.Text,
                  CompanyName = txtCompanyName.Text,
                  ContactName = txtContactName.Text,
                  ContactTitle = txtContactTitle.Text,
                  Address = txtAddress.Text,
                  City = txtCity.Text,
                  Region = txtRegion.Text,
                  PostalCode = txtPostalCode.Text,
                  Country = txtCountry.Text,
                  Phone = txtPhone.Text,
                  Fax = txtFax.Text
              });
  </code></pre>
  
  <p>The above code returns the following error: <strong>"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."</strong></p>
  
  <p>customersGrid is the name of the DataGrid in MainWindow and its structure is as follows. </p>
  
  <pre><code>&lt;DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Margin="8,29,8,8" Name="customersGrid" VerticalAlignment="Stretch" Loaded="customersGrid_Loaded"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding CustomerID}" Header="Κωδικός Πελάτη"/&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding CompanyName}" Header="Όνομα Εταιρίας"/&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding ContactName}" Header="Όνομα Επαφής"/&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding ContactTitle}" Header="Τίτλος Επαφής"/&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding Address}" Header="Διεύθυνση"/&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding City}" Header="Πόλη"/&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding Region}" Header="Περιοχή"/&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding PostalCode}" Header="Ταχυδρομικός Κώδικας"/&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding Country}" Header="Χώρα"/&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding Phone}" Header="Τηλέφωνο"/&gt;
              &lt;DataGridTextColumn Width="Auto" Binding="{Binding Fax}" Header="Fax"/&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  </code></pre>
  
  <p>I suppose that a solution is to set the ItemsSource property of the DataGrid again with all the data but this is not a good choice because the customers data is coming from a wcf service which brings a lot of records. So, what is a good solution to this problem? Thank you in advance.</p>