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

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 techgnome's Avatar
    Join Date
    May 2002
    Posts
    28,510

    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
    91,638

    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-2016

    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
    91,638

    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-2016

    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

  6. #6
    New Member
    Join Date
    Nov 2014
    Posts
    1

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

    Quote Originally Posted by Ajax36 View Post
    I've a datagridview, whenever I display the master record, the associated children records are
    ---------------
    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
    Hi. thanks for that code. I'm not good in english.

    that code helped me but in this form:

    Private Sub Clear_DataGridView()
    Dim i As Integer

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


    I just replaced the "i" value with "0". Because every time you remove a row total rows value will be reduced.
    So all the things you need is that each time you just need to remove the first row.

    Sorry for my bad English writing.

  7. #7
    Addicted Member
    Join Date
    Nov 2013
    Posts
    163

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

    Quote Originally Posted by jmcilhinney View Post
    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.
    I have pretty much the same problem....slightly different. My grid leaves one empty row and I need that one gone as well. I suspect it's because the cursor is sitting in the bottom row, which of course is empty, at the time I clear the grid.

    How do I get rid of that empty row. I tried .remove but I can't figure out what to add to .remove to do it..

    Code:
    Me.DGV1.DataSource = Nothing
            Me.DGV1.Rows.Clear()

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    91,638

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

    Quote Originally Posted by larrycav View Post
    I have pretty much the same problem....slightly different. My grid leaves one empty row and I need that one gone as well. I suspect it's because the cursor is sitting in the bottom row, which of course is empty, at the time I clear the grid.

    How do I get rid of that empty row. I tried .remove but I can't figure out what to add to .remove to do it..

    Code:
    Me.DGV1.DataSource = Nothing
            Me.DGV1.Rows.Clear()
    If you're talking about the new record row at the bottom then that is controlled by the AllowUserToAddNewRows property. It would be a rare thing to want to change at run time though. Why do you think you need to?

    2007-2016

    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

  9. #9
    Addicted Member
    Join Date
    Nov 2013
    Posts
    163

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

    The reason I want to get rid of that row is because of my SAVE button code. I hand potential user errors in the data saving process with this code. This is the section dealing with the DGV. What it doesn't handle is a situation where the user may decide to clear the grid and for God only knows why....then hit the save button, which I do not want them doing if the grid is empty....but you just know someone will do that very thing...

    Code:
    If DGV1.RowCount < (1) And TBoxFlowTestsCustID.Text < 1 Then
                MessageBox.Show("No Captured Flow Data To Save & Customer Data Is Incomplete", _
                                 "C&D Data Archive Process", _
                                 MessageBoxButtons.OK, MessageBoxIcon.None)
                Exit Sub
    
            End If
    
            If DGV1.RowCount < (1) Then
                MessageBox.Show("No Captured Flow Data To Save", _
                                 "C&D Data Archive Process", _
                                 MessageBoxButtons.OK, MessageBoxIcon.None)
                Exit Sub
            End If
    And it's within reason that they may want to save data with only 1 row in the DGV....so that's why its <1

  10. #10
    Addicted Member
    Join Date
    Nov 2013
    Posts
    163

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

    allowuserstoaddrows = false took care of the problem....thank you for that.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    91,638

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

    Quote Originally Posted by larrycav View Post
    The reason I want to get rid of that row is because of my SAVE button code. I hand potential user errors in the data saving process with this code. This is the section dealing with the DGV. What it doesn't handle is a situation where the user may decide to clear the grid and for God only knows why....then hit the save button, which I do not want them doing if the grid is empty....but you just know someone will do that very thing...

    Code:
    If DGV1.RowCount < (1) And TBoxFlowTestsCustID.Text < 1 Then
                MessageBox.Show("No Captured Flow Data To Save & Customer Data Is Incomplete", _
                                 "C&D Data Archive Process", _
                                 MessageBoxButtons.OK, MessageBoxIcon.None)
                Exit Sub
    
            End If
    
            If DGV1.RowCount < (1) Then
                MessageBox.Show("No Captured Flow Data To Save", _
                                 "C&D Data Archive Process", _
                                 MessageBoxButtons.OK, MessageBoxIcon.None)
                Exit Sub
            End If
    And it's within reason that they may want to save data with only 1 row in the DGV....so that's why its <1
    Removing the data entry row seems misguided to me. For one thing, you could just bind the grid and then use the data source to determine whether there is data to save. Even if the grid isn't bound, you can simply test for the presence of rows where IsNewRow is False.

    2007-2016

    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

  12. #12
    Addicted Member
    Join Date
    Nov 2013
    Posts
    163

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

    Quote Originally Posted by jmcilhinney View Post
    Removing the data entry row seems misguided to me. For one thing, you could just bind the grid and then use the data source to determine whether there is data to save. Even if the grid isn't bound, you can simply test for the presence of rows where IsNewRow is False.
    I trust your judgement but answer me this.... If the grid is cleared and that empty row remains, then wouldn't I code to look for IsNewRow = True....rather than false? I mean because the row is there...and I assume it's considered a new row...thus "true".

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    91,638

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

    Quote Originally Posted by larrycav View Post
    I trust your judgement but answer me this.... If the grid is cleared and that empty row remains, then wouldn't I code to look for IsNewRow = True....rather than false? I mean because the row is there...and I assume it's considered a new row...thus "true".
    No, because that row is always there, whether the grid is otherwise empty or not. IsNewRow is a property of each row, so you would look for the existence of at least one row where IsNewRow is False.
    vb.net Code:
    1. If myDataGridView.Rows.Cast(Of DataGridViewRow)().Any(Function(row) Not row.IsNewRow) Then
    2.     'There is at least one row other than the new entry row in the grid.
    3. Else
    4.     'The new entry row is the only row in the grid.
    5. End If

    2007-2016

    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

  14. #14
    Addicted Member
    Join Date
    Nov 2013
    Posts
    163

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

    Quote Originally Posted by jmcilhinney View Post
    No, because that row is always there, whether the grid is otherwise empty or not. IsNewRow is a property of each row, so you would look for the existence of at least one row where IsNewRow is False.
    vb.net Code:
    1. If myDataGridView.Rows.Cast(Of DataGridViewRow)().Any(Function(row) Not row.IsNewRow) Then
    2.     'There is at least one row other than the new entry row in the grid.
    3. Else
    4.     'The new entry row is the only row in the grid.
    5. End If
    Ok...will give that a whirl and see what happens.

    But I'm not crystal clear on how the DGV behaves. When you say that row is always there, based on my setting the allowuserstoaddnewrows = false and as a result, my original code works as I expected.... THEN... when you say that row is always there, that is with the assumption that allowuserstoaddnewrows = true. .......Correct?

    Just trying to grasp how .net works because it's not at all intuitive to me..yet.

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    91,638

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

    Quote Originally Posted by larrycav View Post
    Ok...will give that a whirl and see what happens.

    But I'm not crystal clear on how the DGV behaves. When you say that row is always there, based on my setting the allowuserstoaddnewrows = false and as a result, my original code works as I expected.... THEN... when you say that row is always there, that is with the assumption that allowuserstoaddnewrows = true. .......Correct?

    Just trying to grasp how .net works because it's not at all intuitive to me..yet.
    That is correct. What I'm saying is that you shouldn't be changing that property value just so that you can test for there being no rows in code.

    2007-2016

    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

  16. #16
    Addicted Member
    Join Date
    Nov 2013
    Posts
    163

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

    Quote Originally Posted by jmcilhinney View Post
    That is correct. What I'm saying is that you shouldn't be changing that property value just so that you can test for there being no rows in code.

    Very good...that's what I thought. Just wanted to verify what I thought I learned here was in fact correct. Thank you.

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.