What is the fast way to remove ALL rows in DataGridView-VBForums
Results 1 to 5 of 5

Thread: What is the fast way to remove ALL rows in DataGridView

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    65

    What is the fast way to remove ALL rows in DataGridView

    I've a datagridview, whenever I display the master record, the associated children records are displayed in the datagridview. Bbefore filling in the children records into the datagridview again, I'll first clear all rows in the datagridview.

    I first tried using the following statement and it did not work. I got an error msg: "Index out of range." Mus be non-negative and less than the size of the collection. There are 3 rows of data plus a blank row (at the bottom); hence Me.dgvMaterial_Inventory.Rows.Count = 4.

    Me.dgvMaterial_Inventory.Rows.RemoveAt(dgvMaterial_Inventory.SelectedRows(dgvMaterial_Inventory.Rows .Count - 1).Index)

    I then tried one row at a time using a For-loop as shown below
    However, I got another error indicating "Uncommitted new row cannot be deleted." when trying to delete the 2nd row (with i=1).

    Can someone please tell me why ? If this is not the correct way to do it, can you please advise what is the easy way to delete ALL rows.

    ---------------
    VB Code
    Private Sub Clear_DataGridView()
    Dim i As Integer

    For i = 0 To Me.dgvMaterial_Inventory.Rows.Count - 1
    Me.dgvMaterial_Inventory.Rows(i).Selected = True
    Me.dgvMaterial_Inventory.Rows(i).Dispose()
    Me.dgvMaterial_Inventory.Rows.RemoveAt(Me.dgvMaterial_Inventory.SelectedRows(0).Index)
    Next
    End Sub

  2. #2
    PowerPoster
    Join Date
    May 2002
    Posts
    24,786

    Re: What is the fast way to remove ALL rows in DataGridView

    dgvMaterial_Inventory.Clear ..... seriously, it's that easy. I think.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,341

    Re: What is the fast way to remove ALL rows in DataGridView

    Not quite:
    VB Code:
    1. dgvMaterial_Inventory.Rows.Clear()
    Having said that, if your grid is bound to a DataTable or some other DataSource then you need to clear it, not the grid.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Addicted Member
    Join Date
    Jan 2008
    Posts
    166

    Re: What is the fast way to remove ALL rows in DataGridView

    I tried that but it didn't work. I got an error ("Cannot clear this List")
    HElp me Thanks

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,341

    Re: What is the fast way to remove ALL rows in DataGridView

    Quote Originally Posted by hoobas20
    I tried that but it didn't work. I got an error ("Cannot clear this List")
    HElp me Thanks
    Then your grid must be bound, which I've already addressed. If the grid is bound to a DataTable, which is the most common, then you either need to Clear the Rows collection of the DataTable itself or else unbind it by setting the DataSource to Nothing. Which is better depends on your situation.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.