how to delete selected row in a data gridview

rocks_lp

Member
Joined
Dec 16, 2008
Messages
15
Programming Experience
Beginner
Hi, how do I permanently delete selected row in datagridview as well as permanently deleting it in my .mdb file?.

what i want to do is. after i select a row in datagridwindow and then, when i click the delete button i want the data to be deleted from my datagridwindow and also from the database
 
Select the row
Press the Delete button
Call in code: myBindingSource.EndEdit()
Call in code: myTableAdapter.Update(myDataSet.MyDataTable)
 
Well,

Most likely when you are dealing with a DataGridView you are likely (and recommended) to use a BindingSource. With the binding source you can utilize the Current property to access the currently selected row of the DataGridView and then you follow simple DataSet/DataRow methods to achieve whatever end you want, in this case specifically the Delete() method.

VB.NET:
Private Sub DeleteBtn_Click(sender as object, e as EventArgs) handles DeleteBtn.Click
   If myBindingSource.Current IsNot nothing then
      DirectCast(myBindingSource.Current, DataRowView).Row.Delete()
        'The Above method basically sets the DataRow.RowState to
        ' DataRowState.Deleted
      myBindingSource.EndEdit()
      '  Either
      CommitDeletedRows(myDataTable, myTableAdapter)
      '  or if you want to be dynamic about it
      ' Though it should be noted for such a dynamic method you will
      ' still need to find the appropraite Typed TableAdapter for such a
      ' DataTable, or learn to write a SqlCommand Delete statement on the
      ' Fly
      'if Typeof myBindingSource.DataSource is DataSet then
      '   CommitDeletedRows(DirectCast(myBindingSource.DataSource, _
      '                                     DataSet)(myBindingSource.DataMember))
      'elseif TypeOf myBindingSource is DataTable then
      '   CommitDeletedRows(DirectCast(myBindingSource.DataSource, _
      '                                     DataTable))
      'End If
   End If
End Sub

'Now there are multiple ways to handle the Deletion of flagged DataTable
'DataRows, in order to commit them to the MDB file.  
'And this Of course is easier if you either know the direct DataSet or
'DataTable involved with the deletion.
'As well, this is also affected if you have any child relations that are affected
'by the current table whose row is being deleted.  If a DataRelation exists
'it is necessary to Update the Child Table's rows first, before updating the
'Parent Table's Deleted row.
'Here are some examples that are helpful to peruse in the Online Help
'Documentation for VB 2008

Private Sub CommitDeletedRows(ByVal tbl As DataTable, Adapter as SqlDataAdapter)
    Dim deletes() As DataRow
    Try
         'again this section here is useful for understanding how the
         'child relations are dependent upon each other
         'which again is only possible with a dynamically linked dataadapter
         'for each table
         'For Each rel As DataRelation In tbl.ChildRelations
         '   CommitDeletedRows(rel.ChildTable, GetAdapterForTable(rel.ChildTable))
         'Next
         'End If
         deletes = tbl.Select("", "", DataViewRowState.Deleted)
         if deletes.length >0 then
            Adapter.Update(deletes)
         end if
    catch
    end try
End Sub
This is a much more specific for multiple row handling, as well, it is just as easy to use cjard's method of Adapter.Update(DataTable), which again requires you to know and have the typed DataTable and DataAdapters available, but does not always compensate for child relations. Built in, however, to the DataSetDesigner as it generates the Strongly Typed Dataset, DataTables/DataRows, and DataAdapters, there is a way to use the TableAdapterManager to simply Update the entire DataSet, which is coded to detect DataRelations and process them in the appropriate order, updating Parents before Childs on DataRow Additions, and Childs before Parents on Parent DataRow Deletions.
This is quite helpful when doing a lot of different updates all at once, however, for a single row deletion where it is assured there are no dependent Child DataRelations, you can simply pass a single DataRow to the Adapter.Update.

DW2: Data Walkthroughs 2.0
Which cjard normal has in his signature is extremely helpful for giving some of these same tips with video tutorials which are quite easy to correlate to whatever aim you wish to achieve.

Hope this helps
 

Latest posts

Back
Top