none
clearing a datagridview

    Question

  • Hi! how can i clear all the cells of the datagridview (when are bounded) this is my scenario:

    i use a DataGridView to register products for a sale ticket, but if i decide cancel the ticket i have to clear the products, there is an event, datagridview.clear but it returns "the list cannot be cleared" something like that

    Wednesday, December 28, 2005 2:54 AM

Answers

  • To clear a DataGridView, clear the data to which the DataGridView is bounded.  If that's a DataTable, for instance, it has a Clear() function of its own.
    Wednesday, December 28, 2005 6:57 AM

All replies

  • To clear a DataGridView, clear the data to which the DataGridView is bounded.  If that's a DataTable, for instance, it has a Clear() function of its own.
    Wednesday, December 28, 2005 6:57 AM
  • If you set the RowCount property to 0, all rows will be removed from the DataGridView. This is equivalent to calling the DataGridViewRowCollection.Clear method.
    Friday, June 01, 2007 7:29 AM
  • I tried the above two suggestions, but neither worked.  I tried setting the DataSource of the DataGridView to null and that worked.

     

    dataGridView.DataSource = null;

    • Proposed as answer by sammy1188 Sunday, September 25, 2011 5:51 AM
    • Unproposed as answer by sammy1188 Sunday, September 25, 2011 5:51 AM
    Tuesday, July 10, 2007 5:39 PM
  •                 DataGridView1.RowCount = 1

                    DataGridView1.Rows.Clear()

     

     

    Cuando cambio el  DataGridView1.RowCount se ejecuta automaticamente

    Grid_CurCellChange de DataGridView1, tiene que tener encuenta este evento

     

    When change the DataGridView1.RowCount,  executes Grid_CurCellChange  in  DataGridView1 automatically, this event must have

     

     

    De Juan Grullon

    jqgrullon@gmail.com

     

    Wednesday, November 12, 2008 3:44 PM
  • Okay, say you have a table adapter called someTableAdapter and a data set called someDataSet that has a table called someTable.  Try something that looks like this:

     

    someTableAdapter.FillByClearDataset(someDataSet.someDataSet)

     

    Wednesday, December 10, 2008 12:29 AM
  • al salam alykom
    السلام عليكم
    peace upon with you
    _________________________

    i found a solve for this trick
    follow the steps if you deal with the data like this way

    first you regally have a connection to some database

    2- you got this data into datatable to the datagrid view

    like that

    con as connection
    
    cmd as command
    
    dr as datareader
    
    tb1 as datatable
    
    now you opened the connection
    
    and put the data into the data table like that
    
    cmdon.Connection = cn
            cmdon.CommandType = CommandType.Text
            cmdon.CommandText = "Select * from table "
            dr = cmd.ExecuteReader
            tb1.Clear()
            DataGridView1.DataSource = tb1
            tb1.Load(drgrid)
            DataGridView1.DataSource = tb1
    

    it'll work with you



     
    Tuesday, September 15, 2009 12:37 PM
  • hi, i followed the above suggestion in clearing the datagridview however it doesn't work for me. The scenario of my program is that it will select value from a combobox which will be supplied in the where clause of the program. when i choose a value from this combobox and has no data return it throws an error and this error occurs when I set the datasource of the datagridview....here's my code.

    try
                {
                    SqlConnection conn = new SqlConnection("server =  ourserver; database = mydatabase; uid = users; pwd = pass; ");
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    if (cmbPriority.Text == string.Empty)
                    {
                        cmbPriority.Text = "1";
                    }
                    SqlQuery = "SELECT recid, [Key] as [IO Number], log_date as [Log Date], start_date as [Start Date], finish_date as [Finish Date]," +
                                "status as [Status], priority as [Priority]" +
                                "FROM ZGO_Background_Task WHERE Name = 'FAS' AND Status = 0 AND priority = '"  + cmbPriority.Text + "'" +
                                "ORDER BY Log_Date,Priority";
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(SqlQuery, conn);
                    DataTable dt = new DataTable();
                    dataAdapter.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        dataGridView1.DataSource = dt;
                    }
                    else
                    {
                        
                        MessageBox.Show("No record found.", "", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
       dataGridView1.DataSource = null;

                    }

                
                    conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    Thursday, October 22, 2009 6:48 AM
  • I don't think your problem has anything to do with clearing the datagridview.
    Thursday, October 22, 2009 7:33 AM
  • hi again !!

    i think that's my last post was about clearing the datagridview with the connected mode

    but now i'll show how to clear it with the disconnected mode

    i mean that when using a dataset bounding

    follow :

    dim myview as new dataview
    myview.datasourche=dbs.tables("tablename")
    
    myview.rowfilter=("column1='"$$$$$$$$$$$$$")
    ' the above line describe how to get unexistable case when bounding to disconnect bounding to the datageid
    
    datagridview1.datasource=myview
    
    'so if you want to recall the cells from the table 
    
    datagridview1.datasource=dbs.tables("tablename")
    
    
    'otherwise
    'if you want to filter the datagrid view follow that:
    
    'fixed filter
    myview.rowfilter=("column1='50'")
    
    ' variable filter
    myview.rowfilter=("column1='" & textbox1.text  & "'")
    
    
    datagridview1.datasource=myview
    
    
    i hope it'll be effect
    

    Friday, October 23, 2009 9:43 AM
  • I had same problem.This solution dataGridView.DataSource = "null" (in vb.net) 

    works for me

    • Proposed as answer by Michael_0856 Wednesday, January 05, 2011 6:21 PM
    Thursday, May 13, 2010 12:45 AM
  • I was able to clear the DataGridView with the following code wich clears the data table dt then makes the DataGridView equal to the cleared table

    'Clear data grid view

    dt.Clear()

    dgvDisplay.DataSource = dt

    Thursday, April 21, 2011 11:48 PM
  • Nothing/Null if you use Vb or C#

    dgvDisplay.DataSource = Nothing

    dgvDisplay.DataBind()

    Tuesday, August 16, 2011 2:49 PM
  • Here's what I did

    I made an application which uses a datagridview control.

    I have two DataGridView control

    1.MasterDataGridView

    2.DetailDataGridView

    Database-Northwind

    Serever-->Sql Server 2008

     

    In my mastergridview I pour the data of the customers.

    And whenever I click on the the particular row

    then on the cell click event I wrote

    private void masterDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)

     

    {

    string cutomerId = masterDataGridView.Rows[masterDataGridView.CurrentCell.RowIndex].Cells["CustomerId"].Value.ToString();

     

                string QUERY = "select * from Orders where CustomerId =" + "'"+cutomerId+"'";

     

                SqlDataAdapter detailsDataAdapter = new SqlDataAdapter(QUERY,sqlConnection);

     

                detailsDataAdapter.Fill(ds, "Orders");

     

                detaildataGridView.DataSource = null;

     

                detaildataGridView.DataSource = ds;

     

                detaildataGridView.DataMember = "Orders";

     

                detaildataGridView.AutoGenerateColumns = true;

     

                detaildataGridView.AutoResizeColumns();

            }

     

    Which dispalys the order details of a partricular customers.

    The problem that I was facing that whenever I click on the other records my data was getting appended in the detaildatagridview

    i.e if I select customer="Ramesh" then his orders get displayed in the detailgridview

    but when I click on other customer,his order details used to get displayed before ramesh's data.

     

    So here's what I did

     

     if (detaildataGridView.Rows.Count > 0)

                {

                    ds.Tables["Orders"].Clear();

                }

     

    Just before the code in click event

    i.e

     

    private void masterDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)

            {

                if (detaildataGridView.Rows.Count > 0)

                {

                    ds.Tables["Orders"].Clear();

                }

     

                string cutomerId = masterDataGridView.Rows[masterDataGridView.CurrentCell.RowIndex].Cells["CustomerId"].Value.ToString();

     

                string QUERY = "select * from Orders where CustomerId =" + "'"+cutomerId+"'";

     

                SqlDataAdapter detailsDataAdapter = new SqlDataAdapter(QUERY,sqlConnection);

     

                detailsDataAdapter.Fill(ds, "Orders");

     

                detaildataGridView.DataSource = null;

     

                detaildataGridView.DataSource = ds;

     

                detaildataGridView.DataMember = "Orders";

     

                detaildataGridView.AutoGenerateColumns = true;

     

                detaildataGridView.AutoResizeColumns();

            }

     

    This has worked for me...

    Thursday, November 24, 2011 5:42 AM
  • Try:

    DataGridView.Columns.Clear()

    Thursday, March 01, 2012 8:59 PM
  • If you set the RowCount property to 0, all rows will be removed from the DataGridView. This is equivalent to calling the DataGridViewRowCollection.Clear method.

    "RowCount property cannot be set on a data-bound DataGridView control."

    B. Clay Shannon

    Tuesday, May 08, 2012 6:34 PM
  • If you are using Datagridview with data bound, then you cannot do rows.clear or rowscount = 0

    I have got it working via 

    datagridview.DataSource = Nothing

    Me.MyDeskDataSet.Tables.Clear()

    then Set the new table

    myDA = New SqlCeDataAdapter(cmd)

    myDA.Fill(Me.MyDeskDataSet, "MyTable")
    datagridview.DataSource = Me.MyDataSet.Tables("MyTable").DefaultView

    Wednesday, May 16, 2012 2:28 AM
  • If you are using Datagridview with data bound, then you cannot do rows.clear or rowscount = 0

    I have got it working via 

    datagridview.DataSource = Nothing

    Me.MyDeskDataSet.Tables.Clear()

    then Set the new table

    myDA = New SqlCeDataAdapter(cmd)

    myDA.Fill(Me.MyDeskDataSet, "MyTable")
    datagridview.DataSource = Me.MyDataSet.Tables("MyTable").DefaultView

    Harmik: do you Singh in the Batthtub?

    B. Clay Shannon

    Wednesday, May 16, 2012 3:30 PM
  • just do dataset.Clear();
    Thursday, January 10, 2013 10:49 PM