title: WPF binding error (System.Windows.Data Error: 17)
body: <p>I'm currently learning XAML &amp; WPF and still getting to grips with databinding, mostly just banging my head on the wall until it works.</p>
  
  <p>I am displaying the users table in a DataGrid and using comboboxes to lookup the 'name' values from the corresponding tables for the 'siteid' and 'roleid' values.</p>
  
  <p>The form works as expected, i.e. it displays the datagrid with the comboboxes set correctly - and it also updates the user record just fine - but I am getting the following error message in the output window and its driving me insane:</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 17 : Cannot get 'Name' value (type 'String') from '' (type 'DataRowView'). BindingExpression:Path=Name; DataItem='DataRowView' (HashCode=25172842); target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object') RowNotInTableException:'System.Data.RowNotInTableException: This row has been removed from a table and does not have any data.  BeginEdit() will allow creation of new data in this row.</p>
  </blockquote>
  
  <p><strong>Question 1: What is causing the error?</strong> </p>
  
  <p>The datagrid + comboboxes display correctly so what is wrong with the XAML below? I cannot see it!</p>
  
  <p>My (simple) test project setup is nothing fancy:</p>
  
  <ul>
  <li>VS2013, WPF, .NET45 + entity framework 6.1.3 (latest) &amp; SQL2012</li>
  <li>users table: ID(pk), username, password, SiteID(Fk), RoleID(Fk)</li>
  <li>sites table: ID(pk), name, description</li>
  <li>roles table: ID(pk), name, description</li>
  </ul>
  
  <p>XAML:
      </p>
  
  <pre><code>    &lt;UserControl.Resources&gt;
          &lt;local:TestProjectDataSet x:Key="testProjectDataSet"/&gt;
  
          &lt;CollectionViewSource x:Key="usersViewSource" Source="{Binding Users, Source={StaticResource testProjectDataSet}}"/&gt;
          &lt;CollectionViewSource x:Key="rolesViewSource" Source="{Binding Roles, Source={StaticResource testProjectDataSet}}"/&gt;
          &lt;CollectionViewSource x:Key="sitesViewSource" Source="{Binding Sites, Source={StaticResource testProjectDataSet}}"/&gt;
  
      &lt;/UserControl.Resources&gt;
  
      &lt;Grid Style="{StaticResource ContentRoot}" DataContext="{StaticResource usersViewSource}"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="50"/&gt;
              &lt;RowDefinition Height="*"/&gt;
              &lt;RowDefinition Height="50"/&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;TextBlock Text="User Management:" Style="{StaticResource Heading2}" Grid.Row="0"/&gt;
  
          &lt;DataGrid x:Name="UsersDataGrid" ItemsSource="{Binding}" EnableRowVirtualization="True" Grid.Row="1" RowDetailsVisibilityMode="VisibleWhenSelected" AutoGenerateColumns="False"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn x:Name="idColumn" Width="SizeToHeader" IsReadOnly="True" Header="Id" Binding="{Binding Id}" /&gt;
  
                  &lt;DataGridTextColumn x:Name="usernameColumn" Width="Auto" Header="Username" Binding="{Binding Username}"/&gt;
  
                  &lt;DataGridTextColumn x:Name="passwordColumn" Width="Auto" Header="Password" /&gt;
  
                  &lt;DataGridTemplateColumn x:Name="siteNameColumn" Header="Site" Width="Auto"&gt;
                      &lt;DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;ComboBox IsSynchronizedWithCurrentItem="False"
                                        ItemsSource="{Binding Source={StaticResource sitesViewSource}}"
                                        DisplayMemberPath="Code"
                                        SelectedValuePath="Id"
                                        SelectedValue="{Binding SiteId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                        /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/DataGridTemplateColumn&gt;
  
                  &lt;DataGridTemplateColumn x:Name="roleNameColumn" Header="Role" Width="Auto"&gt;
                      &lt;DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;ComboBox IsSynchronizedWithCurrentItem="False"
                                        ItemsSource="{Binding Source={StaticResource rolesViewSource}}" 
                                        DisplayMemberPath="Name"
                                        SelectedValuePath="Id"
                                        SelectedValue="{Binding RoleId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                        /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/DataGridTemplateColumn&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
  
          &lt;Button Content="Save" Width="100" Height="50" Click="Button_Click" Grid.Row="2" /&gt;
  
      &lt;/Grid&gt;
  
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>Question 2: Is there a better way to do all this??</strong></p>
  
  <p>I had to use DataSets, TableAdapters &amp; CollectionViewSources with some code-behind, e.g.</p>
  
  <pre><code>using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  
  namespace TestProjectUI.Pages.Admin
  {
      /// &lt;summary&gt;
      /// Interaction logic for ManageUsersPage.xaml
      /// &lt;/summary&gt;
      public partial class ManageUsersPage : UserControl
      {
          private TestProjectDataSet _database;
  
          private TestProjectDataSetTableAdapters.UsersTableAdapter _usersAdapter;
          private TestProjectDataSetTableAdapters.RolesTableAdapter _rolesAdapter;
          private TestProjectDataSetTableAdapters.SitesTableAdapter _sitesAdapter;
  
          private CollectionViewSource _usersViewSource;
          private CollectionViewSource _rolesViewSource;
          private CollectionViewSource _sitesViewSource;
  
          public ManageUsersPage()
          {
              InitializeComponent();
          }
  
          private void UserControl_Loaded(object sender, RoutedEventArgs e)
          {
              _database = ((TestProjectDataSet)(FindResource("magazineInventoryDataSet")));
  
              _usersAdapter = new TestProjectDataSetTableAdapters.UsersTableAdapter();
              _usersAdapter.Fill(_database.Users);
              _usersViewSource = ((CollectionViewSource)(FindResource("usersViewSource")));
              _usersViewSource.View.MoveCurrentToFirst();
  
              _rolesAdapter = new TestProjectDataSetTableAdapters.RolesTableAdapter();
              _rolesAdapter.Fill(_database.Roles);
              _rolesViewSource = ((CollectionViewSource)(FindResource("rolesViewSource")));
              _rolesViewSource.View.MoveCurrentToFirst();
  
              _sitesAdapter = new TestProjectDataSetTableAdapters.SitesTableAdapter();
              _sitesAdapter.Fill(_database.Sites);
              _sitesViewSource = ((CollectionViewSource)(FindResource("sitesViewSource")));
              _sitesViewSource.View.MoveCurrentToFirst();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              _usersAdapter.Update(_database.Users);
          }
  
      }
  }
  </code></pre>
  
  <p>I thought it was possible to do all this in pure XAML with no code behind but I've had no luck getting that to work so far (incorrect bindings?!)</p>
  
  <p>If someone could show me a better way, or improve upon the code above it would be most appreciated. </p>
  
  <p>I've recently come back to C# after many years Ruby and now WPF/XAML is kicking my butt!</p>