Share via


Delete last row in datagridview

Question

Thursday, January 16, 2014 12:56 PM | 1 vote

I’m trying to delete the last row of a datagridview programmatically, but I’m unable.  Here’s what I’ve tried so far:

DataGridView1.Rows.RemoveAt(DataGridView1.Rows.Count - 1)

I’ve also tried to select last row and then delete it, but that hasn’t worked either

Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Selected = True

DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Selected = True

For Each row As DataGridViewRow In DataGridView1.SelectedRows
            DataGridView1.Rows.Remove(row)
        Next

I keep getting an error that says “Uncommitted new row cannot be deleted.”

Thanks

All replies (16)

Thursday, January 16, 2014 2:35 PM ✅Answered

I still suggest you do not try to manipulate the grid. As you pull its data, just skip/discard/whatever the data is invalid. This could be checking for IsNewRow, or testing the fields for null or other invalid conditions.

Bob - www.crowcoder.com


Thursday, January 16, 2014 5:49 PM ✅Answered

You're right Molku.  I ended up figuring out how to select all the rows except the last one and then I implemented code that referred to just the selected rows. 

Me.DataGridView1.ClearSelection()
Me.DataGridView1.SelectAll()      
Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Selected = False

Thursday, January 16, 2014 1:01 PM

I always prefer to manipulate the data that is bound to the grid then refresh the grid. Is it an option for you to delete the record from the datasource then re-bind the grid?

I'm not sure without looking but it appears the last row is actually a blank, new row that is there to support inserts. Maybe you could also try deleting second to last row.

Bob - www.crowcoder.com


Thursday, January 16, 2014 1:07 PM

Thanks.  The datagridview is actually not bound to a datasource.  The user enters data in the datagridview then pushes a save button and the the data is saved to a .txt file.  When the user adds info to say, row 5, then a new row 6 appears.  I want to delete this last row 6 because it is messing up the program with null/empty values when the user presses the save button.


Thursday, January 16, 2014 1:43 PM

You could skip processing rows where therow.IsNewRow is true

Bob - www.crowcoder.com


Thursday, January 16, 2014 2:05 PM | 1 vote

To remove the last row that is not data bound

Dim LastRow As DataGridViewRow = (From this In DataGridView1.Rows.OfType(Of DataGridViewRow)() Where Not this.IsNewRow).LastOrDefault
If LastRow IsNot Nothing Then
    DataGridView1.Rows.Remove(LastRow)
End If

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Thursday, January 16, 2014 2:15 PM

This indeed removed the last row, but it also deleted the information in the row above the last one. 


Thursday, January 16, 2014 2:31 PM | 1 vote

I tried the following but got the same "uncommitted new row cannot be deleted" error

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        For Each row As DataGridViewRow In DataGridView1.Rows
            If row.IsNewRow = True Then
                DataGridView1.Rows.Remove(row)
            End If
        Next
    End Sub

Thursday, January 16, 2014 2:48 PM

This indeed removed the last row, but it also deleted the information in the row above the last one. 

Then there is code in your project causing this as this is adnormal behavior and not from anything outside of your code thus you need to re-examine your code to see what is causing this to happen.

Try this example where I took a random project, added the code I provided and it works as expected.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Thursday, January 16, 2014 3:19 PM

dataGridveiw1.Rows[dataGridveiw1.Rows.Count - 1 /* It will return the last index of the GridView*/]

Hamed Shams The consultant and executer of IT projects. Windows and Web programmer based on Microsoft technology.


Thursday, January 16, 2014 3:33 PM

dataGridveiw1.Rows[dataGridveiw1.Rows.Count - 1 /* It will return the last index of the GridView*/]

Hamed Shams The consultant and executer of IT projects. Windows and Web programmer based on Microsoft technology.

Please post in VB.NET, not C# as this is a VB.NET forum.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Thursday, January 16, 2014 3:45 PM

Oh! I didn't attention which forum is it!

So this is that code in VB.NET :

dataGridveiw1.Rows.Remove(dataGridveiw1.Rows.Count - 1 /* It will return the last index of the GridView*/)

Hamed Shams The consultant and executer of IT projects. Windows and Web programmer based on Microsoft technology.


Thursday, January 16, 2014 3:56 PM

Oh! I didn't attention which forum is it!

So this is that code in VB.NET :

dataGridveiw1.Rows.Remove(dataGridveiw1.Rows.Count - 1 /* It will return the last index of the GridView*/)

Hamed Shams The consultant and executer of IT projects. Windows and Web programmer based on Microsoft technology.

You still might like to revise your suggestion i.e. Remove does not want an int, it wants a DataGridViewRow. Of course we can do Rows.RemoveAt(... which is what you might have been going for. Lastly dataGridveiw1 should be DataGridView1 (spell check).

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Monday, January 29, 2018 1:53 PM

thanks its work


Monday, January 29, 2018 2:02 PM

how about remove first row ?


Monday, January 29, 2018 2:06 PM

how about remove first row ?

Hi

Old thread -should have started a new one - however:

The Index of the first row would be 0 (provided there is at least one row), so, you can use .RemoveAt(0).

Regards Les, Livingston, Scotland