title: WPF datagrid and binding of dataset row changed events
body: <p>I'm struggling with using datasets with a WPF datagrid.  Binding and populating the grid works great and is very easy...just create the dataset datasource and drag it onto a window.  Voila!  But when I try to attach a RowChanged event off the dataset, I get the error:</p>
  
  <p>There is already an open DataReader associated with this Command which must be closed first.</p>
  
  <p>When I place a breakpoint inside the event handler, it gets called twice (at least) for some reason, and on the 2nd call, and exception occurs with the description above.  Doesn't make sense to me that it gets called multiple times.</p>
  
  <p>I've read that I could add "MultipleActiveResultSets=true" to my connection string, but this feels like I'm using a SqlServer feature incorrectly to cover up something I've done wrong.</p>
  
  <p>Here's my XAML:</p>
  
  <pre><code>&lt;Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
          xmlns:local="clr-namespace:Test" x:Class="Test.MainWindow"
          Title="MainWindow" Height="350" Width="1141" Loaded="Window_Loaded"&gt;
      &lt;Window.Resources&gt;
          &lt;local:SentinelDBDataSet x:Key="sentinelDBDataSet"/&gt;
          &lt;CollectionViewSource x:Key="changeEventViewSource" Source="{Binding ChangeEvent, Source={StaticResource sentinelDBDataSet}}"/&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid DataContext="{StaticResource changeEventViewSource}"&gt;
          &lt;DataGrid x:Name="changeEventDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="0
                    " RowDetailsVisibilityMode="VisibleWhenSelected"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn x:Name="cameraIDColumn" Binding="{Binding CameraID}" Header="Camera ID" Width="SizeToHeader" IsReadOnly="True"/&gt;
                  &lt;!--&lt;DataGridTextColumn x:Name="changeEventIDColumn" Binding="{Binding ChangeEventID}" Header="Change Event ID" IsReadOnly="True" Width="SizeToHeader"/&gt;
                  &lt;DataGridTextColumn x:Name="backgroundID_RefColumn" Binding="{Binding BackgroundID_Ref}" Header="Background ID Ref" Width="SizeToHeader"/&gt;
                  &lt;DataGridTextColumn x:Name="backgroundID_ChangeColumn" Binding="{Binding BackgroundID_Change}" Header="Background ID Change" Width="SizeToHeader"/&gt;--&gt;
                  &lt;DataGridTemplateColumn x:Name="timeStampColumn" Header="Time Stamp" Width="SizeToHeader" IsReadOnly="True"&gt;
                      &lt;DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;DatePicker SelectedDate="{Binding TimeStamp, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/DataGridTemplateColumn&gt;
  
                  &lt;DataGridTextColumn x:Name="annotationColumn" Binding="{Binding Annotation}" Header="Annotation" Width="SizeToHeader" IsReadOnly="False"/&gt;
                  &lt;DataGridCheckBoxColumn x:Name="validatedColumn" Binding="{Binding Validated}" Header="Validated" Width="SizeToHeader" IsReadOnly="True"/&gt;
                  &lt;DataGridTextColumn x:Name="confidenceColumn" Binding="{Binding Confidence}" Header="Confidence" Width="SizeToHeader" IsReadOnly="True"/&gt;
                  &lt;!--&lt;DataGridCheckBoxColumn x:Name="focusChangedColumn" Binding="{Binding FocusChanged}" Header="Focus Changed" Width="SizeToHeader"/&gt;--&gt;
                  &lt;DataGridCheckBoxColumn x:Name="focusDeltaChangedColumn" Binding="{Binding FocusDeltaChanged}" Header="Focus Delta Changed" Width="SizeToHeader" IsReadOnly="True"/&gt;
                  &lt;!--&lt;DataGridCheckBoxColumn x:Name="brightnessChangedColumn" Binding="{Binding BrightnessChanged}" Header="Brightness Changed" Width="SizeToHeader"/&gt;--&gt;
                  &lt;DataGridCheckBoxColumn x:Name="correlationChangedColumn" Binding="{Binding CorrelationChanged}" Header="Correlation Changed" Width="SizeToHeader" IsReadOnly="True"/&gt;
                  &lt;DataGridCheckBoxColumn x:Name="edgeCorrelationChangedColumn" Binding="{Binding EdgeCorrelationChanged}" Header="Edge Correlation Changed" Width="SizeToHeader" IsReadOnly="True"/&gt;
                  &lt;DataGridCheckBoxColumn x:Name="edgeDensityChangedColumn" Binding="{Binding EdgeDensityChanged}" Header="Edge Density Changed" Width="SizeToHeader" IsReadOnly="True"/&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And here's my MainWindow class:</p>
  
  <pre><code> public partial class MainWindow : Window
      {
          SentinelDBDataSet dataset;
          SentinelDBDataSetTableAdapters.ChangeEventTableAdapter adapter;
  
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              dataset = ((Test.SentinelDBDataSet)(this.FindResource("sentinelDBDataSet")));
  
              // Load data into the table ChangeEvent. You can modify this code as needed.            
              adapter = new Test.SentinelDBDataSetTableAdapters.ChangeEventTableAdapter();            
              adapter.Fill(dataset.ChangeEvent);
  
              System.Windows.Data.CollectionViewSource changeEventViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("changeEventViewSource")));
              changeEventViewSource.View.MoveCurrentToFirst();
  
              dataset.ChangeEvent.RowChanged += ChangeEvent_RowChanged;
  
          }
  
          void ChangeEvent_RowChanged(object sender, System.Data.DataRowChangeEventArgs e)
          {            
              adapter.Update(dataset.ChangeEvent);            
          }
      }
  </code></pre>
  
  <p>Any advice would be greatly appreciated.  If there's a better control to use for interacting with DataSets, I'm willing to look at whatever comes with WPF.  Unfortunatly, this project will not support using commercial tools like Infragistics.</p>
  
  <p>Bryan</p>