none
How to remove all rows in a DataGridView

    Question

  • I have a DataGridView that takes data from a textbox that user inputs. Then, a button that lets user save all rows to database. After saving, I want to remove all rows in the DataGridView. However, the DataGridView always keeps one row undeleted. My code is:

    private void button2_Click(object sender, EventArgs e)

    {

       for (int i = 0; i < dataGridView1.Rows.Count; i++)

       {

           dataGridView1.Rows.RemoveAt(i);

       }

    }

    What is wrong with my code?

    Thanks,

    Tuesday, January 17, 2006 5:33 PM

Answers

  • Thank you for your input.

    I just clear the datasource the DataGridView binds.

    private void button2_Click(object sender, EventArgs e)

    {

    ds.Clear();

    }

    It works.

    Tuesday, January 17, 2006 6:08 PM

All replies

  • The conditional of an if statement is evaluated on every loop; your i is increasing at the same time that Rows.Count is decreasing. This method will always leave at least one row. Use dataGridView1.Rows.Clear() instead. Or even better, clear the rows from the DataSource that the DataGridView is bound to, if any (e.g. myDataTable.Rows.Clear())
    Tuesday, January 17, 2006 5:49 PM
  • Thank you for your input.

    I just clear the datasource the DataGridView binds.

    private void button2_Click(object sender, EventArgs e)

    {

    ds.Clear();

    }

    It works.

    Tuesday, January 17, 2006 6:08 PM
  • You can Also do

    datagridview1.datasource = null;

    • Proposed as answer by Percy Salazar Wednesday, April 25, 2012 9:15 PM
    Wednesday, February 28, 2007 7:09 PM
  • The problem is every time when you remove the dataGridView1.Rows.Count will be reduced by 1.So you have to add the following decremental piece of code to avoid error

     

     

    private void button2_Click(object sender, EventArgs e)

    {

    int rowCount = dataGridView1.Rows.Count;

       for (int i = 0; i < rowCount; i++)

       {

           dataGridView1.Rows.RemoveAt(i);

          --rowCount;

     

       }

    }

     

    Hope this helps.

     

    Thanks,

    V Madhusudanan

    Thursday, June 21, 2007 4:46 PM
  • This works for me

     

    {

        for (int n = 0; n <= dgvAccess.Rows.Count - 1; n++) {

            if (dgvAccess.Rows(0).IsNewRow == false)

                dgvAccess.Rows.RemoveAt(0);

        }

    }

     

    Thursday, July 17, 2008 3:20 PM
  •  

    How about this!

    private
    void ResetDatagridview(DataGridView dgv )      

    {

          while (dgv.Rows.Count > 0)  

          {

             dgv.Rows.RemoveAt(0);

          }

    }

    • Edited by VSA09 Wednesday, May 06, 2009 9:28 PM
    Wednesday, May 06, 2009 9:26 PM
  • this can not work
    because your DataGrid's DataSource is not null
    Please Try to use
        private void ResetDatagridview(DataGridView dgv )      

        if(dgv.datasource != null)
    {
        dgv.datasource = null
    }    
    else
    { dgv.rows.clear();
    }
     
    }


      
    Friday, May 22, 2009 2:43 AM
  •  Public Sub EmptydgvRow()
            For RowCount As Integer = 1 To dgvPoints.Rows.Count
                dgvPoints.Rows.RemoveAt(0)
            Next
     End Sub
    Friday, June 25, 2010 2:53 PM
  • dataGridView1. Rows.Clear()
    Thursday, February 24, 2011 2:23 PM
  • This code clear all rows in datagrid.

     

     for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                        {
                            dataGridView1.Rows.RemoveAt(i);
                            i--;
                            while (dataGridView1.Rows.Count == 0)
                                continue;
                        }

    Wednesday, October 19, 2011 9:59 AM
  • dataGridView1. Rows.Clear()

    The easiest, shortest and surely the most efficient way to do it ;)
    Andrea Antonangeli --- Octet - Ingegneria dei Sistemi - S.r.l. --- Milano (Italy) --- octet.it
    Tuesday, November 08, 2011 1:17 PM
  • The wrong is Rows.Count will decrease when you remove a row, then the loop will change the end condition

     

    This is my code to do this task:

     

                while DataGridView1.Rows.Count>0)

                {

                    if (!DataGridView1.Rows[0].IsNewRow)

                         DataGridView1.Rows.RemoveAt(0);

                }

    • Proposed as answer by Jamiloof Monday, January 16, 2012 4:54 PM
    Monday, January 16, 2012 4:54 PM
  • hey ! man!!! your save my life !! i used "do until" like your example.

    thanks ! good vibes!

    Monday, May 07, 2012 3:27 AM
  • OK... to make story short:

               while (dgvInvoiceDetails.Rows.Count != 0)
                {
                    dgvInvoiceDetails.Rows.RemoveAt(0);
                }

    Wednesday, July 25, 2012 11:46 AM
  • OK... To make story short & done:

                while (dataGridView1.Rows.Count != 0)
                {
                    dataGridView1.Rows.RemoveAt(0);
                }


    Wednesday, July 25, 2012 11:47 AM
  • Definitely the best approach for clearing an unbound datagridview.

    Clayton

    Wednesday, August 01, 2012 2:46 PM
  • It work thank you ....
    Wednesday, August 28, 2013 4:54 AM