|
-
Sep 21st, 2015, 07:44 PM
#1
Re: What is the fast way to remove ALL rows in DataGridView
 Originally Posted by larrycav
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.
-
Sep 21st, 2015, 07:51 PM
#2
Hyperactive Member
Re: What is the fast way to remove ALL rows in DataGridView
 Originally Posted by jmcilhinney
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".
-
Sep 21st, 2015, 08:14 PM
#3
Re: What is the fast way to remove ALL rows in DataGridView
 Originally Posted by larrycav
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:
If myDataGridView.Rows.Cast(Of DataGridViewRow)().Any(Function(row) Not row.IsNewRow) Then 'There is at least one row other than the new entry row in the grid. Else 'The new entry row is the only row in the grid. End If
-
Sep 21st, 2015, 08:24 PM
#4
Hyperactive Member
Re: What is the fast way to remove ALL rows in DataGridView
 Originally Posted by jmcilhinney
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:
If myDataGridView.Rows.Cast(Of DataGridViewRow)().Any(Function(row) Not row.IsNewRow) Then
'There is at least one row other than the new entry row in the grid.
Else
'The new entry row is the only row in the grid.
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.
-
Sep 21st, 2015, 08:43 PM
#5
Re: What is the fast way to remove ALL rows in DataGridView
 Originally Posted by larrycav
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.
-
Sep 21st, 2015, 08:49 PM
#6
Hyperactive Member
Re: What is the fast way to remove ALL rows in DataGridView
 Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|