简体   繁体   中英

Empty a DataGridView

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!

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.

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 :)

我猜想您是手动添加行(dataGridView1.Rows.Add),在这种情况下,将DataSource设置为null不会做任何事情。

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM