title: Keeping a datagrid in sync with a mysql database
body: <p>I'm experimenting with databinding in WPF</p>
  
  <p>I have this datagrid that is bound to a mysql view, it's gonna be read only, but I'd like it to automatically refresh when changes are made to the database.<br>
  Couldn't find anything useful on google and I have no idea where to start.  </p>
  
  <p>This is the dataprovider class.</p>
  
  <pre><code>class ConDataProvider
  {
      private MySqlDataAdapter adapter;
      private Data data;
      private DataTable table;
  
      public ConDataProvider(string query)
      {
          data = new Data();
          table = new DataTable("con_FullGrid");
          adapter = data.getAdapter(query);
          adapter.Fill(table);
      }
      public DataView GetDView()
      {
              dv = new DataView();
              dv.Table = table;
              return dv;
      }
  }
  </code></pre>
  
  <p>And the XAML for the window</p>
  
  <pre><code>&lt;Window x:Class="HelioWPF_Alpha01.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:local="clr-namespace:HelioWPF_Alpha01"
      xmlns:sys="clr-namespace:System;assembly=mscorlib"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="676" Width="924" MinHeight="300" MinWidth="463" Background="{x:Null}"&gt;
  &lt;Window.Resources&gt;
      &lt;ObjectDataProvider x:Key="ConDataProvider" ObjectType="{x:Type local:ConDataProvider}"&gt;
          &lt;ObjectDataProvider.ConstructorParameters&gt;
              &lt;sys:String&gt;SELECT * FROM con_FullGrid&lt;/sys:String&gt;
          &lt;/ObjectDataProvider.ConstructorParameters&gt;
      &lt;/ObjectDataProvider&gt;
      &lt;ObjectDataProvider x:Key="btable" ObjectInstance="{StaticResource ConDataProvider}"
                          MethodName="GetDView"/&gt;
  
  &lt;/Window.Resources&gt;
  &lt;Grid Name="mainGrid" DataContext="{Binding Source={StaticResource btable}" KeyDown="mainGrid_KeyDown" FlowDirection="LeftToRight" &gt;
      &lt;DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" Name="dataGrid1" FrozenColumnCount="20" RowHeight="25" Grid.ColumnSpan="4" ColumnWidth="*" MouseDoubleClick="dataGrid1_MouseDoubleClick" SelectionMode="Single" Grid.Row="2" Grid.Column="1" TabIndex="1" GotKeyboardFocus="dataGrid1_GotKeyboardFocus"&gt;
          &lt;DataGrid.AlternatingRowBackground&gt;
              &lt;LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"&gt;
                  &lt;GradientStop Color="Black" Offset="0" /&gt;
                  &lt;GradientStop Color="#16436DF8" Offset="0" /&gt;
              &lt;/LinearGradientBrush&gt;
          &lt;/DataGrid.AlternatingRowBackground&gt;&lt;/DataGrid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>What I have been doing is kind of faking it by reinitializing the ConDataProvider class in behind code and rebinding the datagrid on various events.
  But it's an ugly hack.<br>
  I wouldn't mind redesigning the whole thing if I have to.  </p>
  
  <p>The Data class just contains the connection string and some methods to interact with the database.
  You can see it here: <a href="http://pastebin.com/m9HLfwEQ" rel="nofollow">http://pastebin.com/m9HLfwEQ</a></p>
  
  <p>Thanks in advance.<br>
  Hope to get some good tips.</p>