0

I have this DataTable called ProductTablethat have 3 columns that serves as the source for the DataGridView ProductGrid.

When clicking on the button DeleteRow, the user should delete the row in ProductTable that correspond the selected row in ProductGrid

I tried with

Private Sub DeleteRow_Click(sender As Object, e As EventArgs) Handles DeleteRow.Click

    Dim i As Integer

    For i = 0 To 100
        Try
            ProductTable.Rows(i).Delete()
        Catch ex As Exception
        End Try
    Next

End Sub

But I'm missing something obvious : The condition that allows me to pick the row in the DataTable that would correspond to ProductGrid.SelectedCells(0).Value.ToString().

But I don't know how to do it, as ProductTable.Rows(i).Columns(0).Value doesn't work since Columns isn't a member of the Row object.

What it looks like

6
  • "correspond the selected row in ProductGrid", How do you decide which row is selected? is the button DeleteRow inside the ProductGrid? Commented Feb 6, 2019 at 14:38
  • The selected row is the row on which the user clicked. DeleteRow isn't inside ProductGrid, but bellow in the same userform. Commented Feb 6, 2019 at 14:57
  • How do you know which row is selected when user clicks a button outside the ProductGrid? Is there a checkbox in every row? Can you show with screenshot? It would be way easier if you add a new column so that a delete button corresponds to each row. Commented Feb 6, 2019 at 14:59
  • I added a screenshot, with redacted content of course. The blue cells correspond to the selected row in the datagridview. Commented Feb 6, 2019 at 15:09
  • Is the grid using a DataSource? Show how the grid is getting its data. Commented Feb 6, 2019 at 16:17

2 Answers 2

3

You may have a problem deleting a row from the bound DataTable using the grids “selected” row index if the grid is “Sorted or Filtered”. I am betting the wrong rows will get deleted from the DataTable when the grid is sorted or filtered.

To fix this, I suggest using the grids DataBoundItem from the selected row. This should ensure the proper row is removed from the DataTable. Something like below…

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
  if (DataGridView1.SelectedRows.Count() > 0) Then
    Dim row As DataGridViewRow = DataGridView1.SelectedRows(0)
    If (Not row.IsNewRow) Then
      row.DataBoundItem.Delete
      ' You can also directly delete the row from the grids rows collection
      ' which will automatically map to the proper row in the table
      'DataGridView1.Rows.RemoveAt(row.Index)
    End If
  End If
End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

Exactly what I wanted, but I didn't know about the DataBoundItem objet, thanks !
I cannot get this to compile with Option Strict on. It is the row.DataBoundItem.Delete It seems to need a cast but I can't figure to what. The MS docs show the Grid bound to an ArrayList of a class and the cast is to the underlying class.
@Mary I am guessing there is some “conversion/widening/narrowing” or unseen casting between the DataGridViewRow and the tables DataRow we are deleting. I am just guessing as this appears to be the only “conversion/narrowing” going on that would cause the compiler to complain. Fortunately, you can use the commented line of code below. DataGridView1.Rows.RemoveAt(row.Index) It works with Option Strict on.
For Option Strict on (which it should be) the line should read... DirectCast(row.DataBoundItem, DataRowView).Delete()
The commented area worked for me DataGridView1.Rows.RemoveAt(row.Index). :)
1

If you just want to delete single selected row

DataGridView1.CurrentRow.Delete()

1 Comment

I want to delete a DataTable row, not the DataGridView one :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.