This is a migrated thread and some comments may be shown as answers.

How to Clear GridView

8 Answers 3148 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 28 Jul 2008, 09:31 PM
Is there a way to clear all of the data from the WinForm GridView?  I can rebind to a new empty DataSource, but then there are no column headings.  Ideally I just want to leave the columns as is and just clear the rows underneath.  Is there a way to do this?

Thanks.
Dan

8 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 31 Jul 2008, 11:23 AM
Hi Dan,

Thank you for the question.

In order to clear the RadGridView rows, you should iterate through the Rows collection and remove them one by one:
while (this.radGridView1.Rows.Count > 0)  
{  
    this.radGridView1.Rows.RemoveAt(this.radGridView1.Rows.Count - 1);  

If you have additional questions, feel free to contact me.

Regards,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Atul Nisar
Top achievements
Rank 1
answered on 15 Nov 2009, 09:01 PM
This method is very inefficient. I have over 100K rows in the grid. It takes forever for this loop .
Rows.Clear()  throws exception :(
0
Julian Benkov
Telerik team
answered on 18 Nov 2009, 01:26 PM
Hello Atul Nisar,

The ADO.NET DataTable does object not support the clear operation. In order to resolve the issue, you must set AutoGenerateColumns property to false and rebind to an empty DataSource.

this.radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false;
this.radGridView1.DataSource = new DataTable(); //replace new DataTable() with your data source object

I hope this helps. Do not hesitate to write us back if you have additional questions.

Best wishes,
Julian Benkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Andrew Shyliuk
Top achievements
Rank 1
answered on 24 Mar 2010, 03:39 PM
Hello

Could I ask what prevent you to include this code into method Rows.Clear()?
0
Julian Benkov
Telerik team
answered on 29 Mar 2010, 09:23 AM
Hello Andrew Shyliuk,

Thank you for the question.

RadGridView supports binding to different types of objects which implement IList, IBindingList interfaces and can not contain code for specific scenarios such as the scenario of binding to a DataTable object. If you bind to an IBindingList custom business object which supports the Clear operation, RadGridView will not throw the data exception.

Best wishes,
Julian Benkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Silvio Silva Junior
Top achievements
Rank 2
answered on 04 May 2010, 07:57 PM
Nice Julian!!!
Nice!
0
Parth
Top achievements
Rank 1
answered on 15 Apr 2019, 04:39 PM

       
radGridView.DataSource = Nothing

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Apr 2019, 11:26 AM
Hello, guys,     

Setting the DataSource property to null is an appropriate solution for clearing the grid. An alternative approach is to clear the Rows and Columns collection as well: 

this.radGridView1.Rows.Clear();
this.radGridView1.Columns.Clear();

Feel free to use this approach which suits your requirement best.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Dan
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Atul Nisar
Top achievements
Rank 1
Julian Benkov
Telerik team
Andrew Shyliuk
Top achievements
Rank 1
Silvio Silva Junior
Top achievements
Rank 2
Parth
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or