title: DataGrid and Observable Collection in WPF
body: <p>I have a datagrid like below in my WPF application. </p>
  
  <pre><code>&lt;Window x:Class="MyApp.TestWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;Grid&gt;
      &lt;DataGrid x:Name="dgTest"  ItemsSource="{Binding TestSource}" AutoGenerateColumns="False" &gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTemplateColumn Width="125" &gt;
                         &lt;DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                                        &lt;TextBox Text="{Binding Column1}"&gt;&lt;/TextBox&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/DataGridTemplateColumn&gt;
                  &lt;DataGridTemplateColumn Width="500" &gt;
                      &lt;DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;TextBox Text="{Binding Column2}"&gt;&lt;/TextBox&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/DataGridTemplateColumn&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
           &lt;Button Click="SaveButton_Click"&gt;Save&lt;/Button&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>I am binding it using the following code. Now my requirement is when the user enters some text into these textboxes inside datagrid and click on save button, it should update the database. How can I achieve this?</p>
  
  <pre><code>namespace MyApp
  {
      public partial class TestWindow: Window
      {
          private ObservableCollection&lt;Test&gt; _testSource
          public ObservableCollection&lt;Test&gt; TestSource
          {
              get
              {
                  return _testSource;
              }
              set
              {
                  _testSource = value;
                  OnPropertyChanged("TestSource");
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
          private void OnPropertyChanged(string propName)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
              }
          }
  
          public TestWindow()
          {
              InitializeComponent();
              TestSource= new ObservableCollection&lt;Test&gt;();
               string strConnString = Application.Current.Properties["connectionStr"].ToString();
               SqlConnection con = new SqlConnection(strConnString);
              SqlCommand cmd = new SqlCommand("SELECT Column1,Column2 FROM MyTable", con);
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              DataTable dtTest = new DataTable();
              da.Fill(dtTest);
              foreach (DataRow row in dtTest)
              {
                  Test cd = new Test();
                  cd.Column1 = row["Column1"].ToString();
                  cd.Column2 = row["Column2"].ToString();
                  TestSource.Add(cd);
              }
              this.DataContext = this;
          }
  
          private void SaveButton_Click(object sender, RoutedEventArgs e)
          {
               // here I need to get the updated ObservableCollection, but now it is showing old data
               foreach Test t in TestSource)
              {
                  string a = t.Column1;
                  string b = t.Column2;
              }
          }
      }
  
      public class Test: 
      {
          public string Column1{ get; set; }
          public string Column2{ get; set; }
      }
  }
  </code></pre>
  
  <p>Thanks</p>