How to Clear GridView

9 posts, 0 answers
  1. Dan
    Dan avatar
    2 posts
    Member since:
    Jul 2008

    Posted 28 Jul 2008 Link to this post

    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
  2. Nikolay
    Admin
    Nikolay avatar
    1805 posts

    Posted 31 Jul 2008 Link to this post

    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.
  3. Atul Nisar
    Atul Nisar avatar
    3 posts
    Member since:
    Oct 2009

    Posted 15 Nov 2009 Link to this post

    This method is very inefficient. I have over 100K rows in the grid. It takes forever for this loop .
    Rows.Clear()  throws exception :(
  4. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 18 Nov 2009 Link to this post

    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.
  5. Andrew Shyliuk
    Andrew Shyliuk avatar
    26 posts
    Member since:
    Nov 2009

    Posted 24 Mar 2010 Link to this post

    Hello

    Could I ask what prevent you to include this code into method Rows.Clear()?
  6. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 29 Mar 2010 Link to this post

    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.
  7. Silvio Silva Junior
    Silvio Silva Junior avatar
    100 posts
    Member since:
    Mar 2010

    Posted 04 May 2010 Link to this post

    Nice Julian!!!
    Nice!
  8. Parth
    Parth avatar
    1 posts
    Member since:
    May 2017

    Posted 15 Apr 2019 Link to this post


           
    radGridView.DataSource = Nothing

  9. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    4004 posts

    Posted 16 Apr 2019 Link to this post

    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.
Back to Top