We help IT Professionals succeed at work.
Get Started

VB.NET DataGridView Button Not Removing Current Row

Last Modified: 2014-05-02

I have a DataGridView with records in it. I have added an Unbound column to the end of the DataSet that deletes the record and updates a SQL table.

I am able to click the delete button and it remove my DataGridView Row, however the SQL inserts the SQL for the record below the one I have selected the delete on.

My code is below is anyone able to explain this behaviour to me?

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        'Declare Variables
        Dim strSQL As String
        Dim cmd As SqlCommand
        Dim i As Integer
        'Dim msg As String = _
        'String.Format("Editing Cell at ({0}, {1})", _
        'e.ColumnIndex, e.RowIndex)

        i = e.ColumnIndex

        If i <> 8 Then

            DataGridView1.Columns(i).ReadOnly = True



            strSQL = "INSERT INTO [MyDB].[dbo].[Database]" & _
                    " VALUES ('" & DataGridView1.Rows(e.RowIndex).Cells(0).Value() & "','" & DataGridView1.Rows(e.RowIndex).Cells(1).Value() & "','" & DataGridView1.Rows(e.RowIndex).Cells(2).Value() & "','" & DataGridView1.Rows(e.RowIndex).Cells(3).Value() & "','" & DataGridView1.Rows(e.RowIndex).Cells(4).Value() & "','Removed','" & Date.Now.ToString & "','" & DataGridView1.Rows(e.RowIndex).Cells(7).Value() & "')"

            cmd = New SqlCommand(strSQL, conn)


        End If

    End Sub

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE