-
Sep 8th, 2006, 10:51 PM
#1
Thread Starter
Lively Member
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
-
Sep 8th, 2006, 10:52 PM
#2
Re: What is the fast way to remove ALL rows in DataGridView
dgvMaterial_Inventory.Clear ..... seriously, it's that easy. I think.
-tg
-
Sep 9th, 2006, 03:15 AM
#3
Re: What is the fast way to remove ALL rows in DataGridView
Not quite:
VB Code:
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.
-
May 28th, 2008, 01:48 AM
#4
Addicted Member
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
-
May 28th, 2008, 01:52 AM
#5
Re: What is the fast way to remove ALL rows in DataGridView
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.
-
Nov 9th, 2014, 01:24 PM
#6
New Member
Re: What is the fast way to remove ALL rows in DataGridView
Originally Posted by Ajax36
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.
-
Sep 21st, 2015, 06:27 PM
#7
Hyperactive Member
Re: What is the fast way to remove ALL rows in DataGridView
Originally Posted by jmcilhinney
Not quite:
VB Code:
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()
-
Sep 21st, 2015, 06:52 PM
#8
Re: What is the fast way to remove ALL rows in DataGridView
Originally Posted by larrycav
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?
-
Sep 21st, 2015, 07:20 PM
#9
Hyperactive Member
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
-
Sep 21st, 2015, 07:28 PM
#10
Hyperactive Member
Re: What is the fast way to remove ALL rows in DataGridView
allowuserstoaddrows = false took care of the problem....thank you for that.
-
Sep 21st, 2015, 07:44 PM
#11
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
#12
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
#13
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
#14
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
#15
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
#16
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
|