l behavior since I am not really deleting the rows from the database, only from the dataset. If you uncomment out the lines where noted, the deletes will actually occur in the database also.
Public Sub Delete_Row(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
'''' Retrieve the ID of the product to be deleted Dim ProductID As system.Int32 = System.Convert.ToInt32(E.Item.Cells(0).Text)
dtgProducts.EditItemIndex = -1
'''' Create and load a DataSet Connect() Dim adapter As New SqlDataAdapter(strSql, objConn) Dim ds As New DataSet() adapter.Fill(ds, "Products") Disconnect()
'''' Mark the product as Deleted in the DataSet Dim tbl As DataTable = ds.Tables("Products") tbl.PrimaryKey = New DataColumn() _ { _ tbl.Columns("ProductID") _ } Dim row As DataRow = tbl.Rows.Find(ProductID) row.Delete()
'''' Reconnect the DataSet and delete the row from the database ''''----------------------------------------------------------- '''' Following section commented out for demonstration purposes ''''Dim cb As New SqlCommandBuilder(adapter) ''''Connect() ''''adapter.Update(ds, "Products") ''''Disconnect() ''''----------------------------------------------------------- '''' Display remaining rows in the DataGrid dtgProducts.DataSource = ds.Tables("Products") dtgProducts.DataBind()
End Sub
End Class
You may run the program here. You may download the code here.