title: Update DataGridView based on Combobox value
body: <p>I have a form with a combo box and a datagridview control. The combo box is populated by a database table, and stores a Transaction ID.</p>
  
  <p>When the combo box is updated, the Datagridview is bound to a database table, and displays the records associated with the Transaction ID from the combo box.</p>
  
  <pre><code>cboTransaction_SelectionChangeCommitted(object sender, EventArgs e)
  {
      int id = Convert.ToInt32(cboTransaction.SelectedValue);
      db.Items.Where(i =&gt; i.TransactionId == id).Load();
      bs.DataSource = db.Items.Local.ToBindingList();
      dgv.DataSource = bs;
  }
  </code></pre>
  
  <p>For the first record, this works fine. A user can select a transaction ID and add/edit Item records in the DataGridView without issue.</p>
  
  <p>However, when a user selects a <strong>different</strong> Transaction from the combo box, things get a bit messier. Using the code above, the DataGridView does not clear the rows with the old ID number, but shows the old records + any records for the newly selected Transaction. I assumed that the Where() clause would limit the DataGridView to those items where the Transaction ID matched.</p>
  
  <p>If I add <code>db.SaveChanges();</code> and  <code>dgv.Rows.Clear();</code> to the beginning of that block, the rows are cleared successfully, however changes are not saved to the database.</p>
  
  <p>I have also tried </p>
  
  <pre><code>db.Items.Load();
  bs.DataSource = db.Items.Local.ToBindingList().Where(i =&gt; i.TransactionId == id);
  </code></pre>
  
  <p>This binds the Datagridview to the correct Transaction, but does not allow the user to add any <strong>new</strong> records.</p>
  
  <p>How can I ensure that the datagridview displays only the records related to the desired transaction, while still allowing the user to add/edit records?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I created a test form with a textbox, button and datagridview for testing, and have the same results. The Datagridview has a Datasource of the database table being edited.</p>
  
  <pre><code>public partial class frmTest : Form
  {
  private BindingSource bs = new BindingSource();
  private UniformDataContainer db = new UniformDataContainer();
  public frmTest()
  {
      InitializeComponent();
      dataGridView1.DataSource = bs;
  }
  
  private void button1_Click(object sender, EventArgs e)
  {
      int id = Convert.ToInt32(textBox1.Text.ToString());
      db.Items.Where(i =&gt; i.TransactionId == id).Load();
      bs.DataSource = db.Items.Local.ToBindingList();
  
      bs.ResetBindings(false);
  }
  
  private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
  {
      db.SaveChanges();
  }
  }
  </code></pre>
  
  <p>While debugging the code, I found that <code>db.Items.Local</code> still contained the old rows, and did not seem to clear.</p>