views:

188

answers:

4

Hi all, 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.

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

As there seemed to be some ambiguity, here is the code in full:

        DataGridViewButtonColumn custAddJobSelect = new DataGridViewButtonColumn();
        Datatable dt = new Datatable();
        // ---> This was the culprit ---> 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;
        }
    }

Thanks for your patience, hope this helps someone else!

A: 

Setting the DataSource to null should do the trick, make sure that your particular set of code is being hit when your try a new search. For instance, plug it into its own procedure and call the procedure at the beginning of the search to ensure that the object is cleared. Truthfully, unless you have a reason for not clearing the DataSource each time your search button is pressed, then there is probably no point in having conditional logic surrounding that section of code.

Zensar
A: 

What exactly is your data source? Is it a DataTable, a list of objects/entities, or do you add the rows to the DataGridView manually?

This might be stating the obvious, but if your data source is a DataTable or list, you should make sure that you are removing all items from the list before adding the new search results to it, instead of focusing on the DataGridView itself.

I'm assuming that your current DataSource = null solution doesn't work, and this would be an obvious explanation :)

Bernhof
A: 

I would guess you are adding the rows manually (dataGridView1.Rows.Add), in which case setting the DataSource to null wont do anything.

SwDevMan81
A: 

Question now answered, seemed to be a problem with:

custDataGridView.DataSource = null;

Take a look for full code, if you can make any other suggestions please do :)

David Archer