title: C# dataset clear method
body: <p>I'm new to C#. I want to create a windows form with a combobox and a dataGridView. I fill the dataGridView based on the selected value from the combobox. So far so good. But when I change the value of the combobox, and try to reload the dataGridView's contents like so:</p>
  
  <pre><code>private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  {
      if (comboBox1.SelectedValue != null) 
      { 
          if (ds.Tables.Contains("Leagues"))
          { 
              ds.Tables["Leagues"].Clear(); // &lt;-- error happens here 
          }
          leagues_adapter.SelectCommand.Parameters[0].Value = comboBox1.SelectedValue;
          main.InsertCommand.Parameters[0].Value = comboBox1.SelectedValue; 
          leagues_adapter.Fill(ds, "Leagues"); 
      } 
  }
  </code></pre>
  
  <p>When <code>myDataSet.Tables["myTable"].Clear()</code> is called, I get on a DataGridView Default Error Dialog the following: </p>
  
  <blockquote>
    <p>System.IndexOutOfRangeException: Index 0 does not have a value
    at System.Windows.Forms.ConcurrencyManager.get_Item(Int32
    index) at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32
    rowIndex). To replace this default dialog please handle the DataError event.</p>
  </blockquote>
  
  <p>I also have an OK button.</p>
  
  <p>This happens only when I want to change the contents of the combobox from a certain value to some other value. 
  However, if I hit the OK button lots of times, I see that with every hit, an element from a row and a column is removed one by one, and when I consume all the elements with the OK button, I finally get what I wanted: the dataGridView with the new data in it. Please help me, any answer is appreciated. Thanks in advance.</p>