Results 1 to 16 of 16

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

Hybrid View

  1. #1
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    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".

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Hyperactive Member
    Join Date
    Nov 2013
    Posts
    292

    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
  •  



Click Here to Expand Forum to Full Width