How to delete all rows of a DataGridView when loading a form?

How to delete all datagridview rows except column headers?

I tried:

dataGridView1.Rows.clear(); 

but that will not work.

I tried iterating over the lines and using the RemoveAt method, but does not delete all the lines:

 private void Form5_Load(object sender, EventArgs e) { dataGridView1.AutoGenerateColumns = true; SqlConnection con = new SqlConnection(@"Data Source=.\myserver;Initial Catalog=test;Integrated Security=True"); adapter = new SqlDataAdapter("SELECT id as [#], description as [Description], unit as [Unit], amount as [Amount], unitPrice as [Unit Price], total as [Total] FROM tbl_poMaterials", con); adapter.SelectCommand.CommandType = CommandType.Text; DataTable tb = new DataTable(); adapter.Fill(tb); SqlCommandBuilder builder = new SqlCommandBuilder(adapter); dataGridView1.DataSource = tb; dataGridView1.Columns[0].Width = 30; dataGridView1.Columns[0].ReadOnly = true; dataGridView1.Columns[1].Width = 660; for (int i = 0; i < tb.Rows.Count; i++) { tb.Rows.RemoveAt(i); } } 
+8
source share
9 answers

This worked for me:

 do { foreach (DataGridViewRow row in dataGridViewError.Rows) { try { dataGridViewError.Rows.Remove(row); } catch (Exception) { } } } while (dataGridViewError.Rows.Count > 1); 
+13
source

You need to clear the DataSource or DataTable, not the datagridview.

 dataGridView.DataSource = null; dataGridView.Refresh(); 

or

 dataTable.Clear(); dataGridView.Refresh(); 
+8
source

I use

 dataGridViewResult.Rows.Clear(); 

to clear all rows without deleting columns.

+5
source

If your grid is bound to a DataTable or some other DataSource, then you need to clear it, not the grid, otherwise the Rows.clear () method will be the right and best way to do this.

+3
source

In the same issue, I tried several methods, but could not succeed. As one answer says:

 for(int i = 0; i < myDataGridView.Rows.Count; i++) { myDataGridView.Rows.RemoveAt(i) } 

will actually delete the line, but the next line will be moved to the previous line! So the above approach removes half the number of rows! Therefore, you need to repeat the action until it becomes zero!

Alternatively, try removing from the last line to the first. He works!

 for(int i = myDataGridView.Rows.Count - 1; i >= 0; i--) { myDataGridView.Rows.RemoveAt(i); } 
+3
source

// this should work

 int rowCount = dataGridView1.Rows.Count; for (int i = 0; i < rowCount; i++) { dataGridView1.Rows.RemoveAt(0); } 

Your solution did not work, because every time the for loop checks the condition i < rowCount , rowCount already decrease by one as a result of dataGridView1.Rows.RemoveAt(i) . Thus, only half of the lines would be deleted.

+1
source

while(dataGridView1.Rows.Count >1) { dataGridView1.Rows.RemoveAt(0); }

+1
source
 int rowCount = dtg.Rows.Count; for (int i = rowCount - 1; i >= 0; i--) { DataGridViewRow dr = dtg.Rows[i]; dtg.Rows.Remove(dr); } 
0
source

Zero the row and column counts and update the dataset.

 dataGrid.Rows.Count = 0; 

dataGrid.Refresh();

0
source

Source: https://habr.com/ru/post/905431/


All Articles