title: Empty a DataGridView
body: <p>I have a datagridview in C# that fills with customers based upon search criteria when a person clicks "search". The problem I'm having is that when The person clicks on the search button more than once to search for customers, or if the user wants to do a different search, all of the details from the last search are still in the datagridview. The major problem right now is not that the columns are not being removed or extras are being added, but that the column index of the button within the datagridview becomes "0" after searching more than once. </p>
  
  <p>Now that I have figured out the problem, I think I'll keep the full details up here in case anyone would like to look at the details in future. The problem was, it seems, that when I remove the custDataGridView.DataSource = null; part, everything seemed to work</p>
  
  <p>As there seemed to be some ambiguity, here is the code in full:</p>
  
  <pre><code>        DataGridViewButtonColumn custAddJobSelect = new DataGridViewButtonColumn();
          Datatable dt = new Datatable();
          // ---&gt; This was the culprit ---&gt; custDataGridView.DataSource = null;
          dt.Rows.Clear();
  
          #region SQL connection String
          ...//CustQuery is the SQL stuff, conn is connection to DB.
          #endregion
  
          da = new System.Data.SqlClient.SqlDataAdapter(CustQuery, conn);
          da.Fill(dt);
  
          conn.Close();
  
          custDataGridView.DataSource = dt;
  
  
          if (AddJobGone == false)
          {
  
              AddJobGone = true;
              custAddJobSelect.DisplayIndex = 0;
              custDataGridView.Columns.Add(custAddJobSelect);
          }
  
          private void custDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
      {
          if (e.ColumnIndex == this.custDataGridView.Columns.Count - 1)
          {
              string addJobsCustNo = custDataGridView.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
              txtAddJobsCustNo.Text = addJobsCustNo;
              pnlAddJobSearchCustomer.Visible = false;
          }
      }
  </code></pre>
  
  <p>Thanks for your patience, hope this helps someone else!</p>