Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET: Clear DataGridView

I've tried -

DataGridView1.DataSource=Nothing 

and

DataGridView1.DataSource=Nothing DataGridView1.Refresh() 

and

DataGridView1.RefreshEdit() 

None of them works..

I've written a method that sets the DataSource of the DataGridView when executed. but each time i execute it, it replicates the data with new value and appends it to the previous contents of the DGV.. I wanna clear the content and then add the values.. Is that possible?

like image 485
Bibhas Debnath Avatar asked Feb 07 '10 15:02

Bibhas Debnath


People also ask

What is DataGridView in VB net?

The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor.


2 Answers

If the DataGridView is bound to any datasource, you'll have to set the DataGridView's DataSource property to Nothing.

If the DataGridView is not bound to any data source, this code will do the trick:

DataGridView.Rows.Clear() 
like image 185
Alex Essilfie Avatar answered Sep 29 '22 04:09

Alex Essilfie


For unbound cases note that:

DataGridView.Rows.Clear() 

leaves the Columns collection in place.

DataGridView.Columns.Clear() 

..will remove all the columns and rows. If you are using the DGV unbound, and on next use the columns change, clearing the Rows may not be adequate. For library code clear all the columns before adding columns.

like image 43
rheitzman Avatar answered Sep 29 '22 03:09

rheitzman