Deleting rows from a DataTable bound to a DataGridView

daviddoria

Member
Joined
Jan 11, 2007
Messages
13
Programming Experience
Beginner
I have a table in a database with a few columns, one of which is AssociateName. It exists as a strongly typed dataset in my project. I populate a list box with the associate names. When I click on a name in the list box, I want to show all of the records for that person in the DataGridView. Then I want to be able to delete one of the rows in the DataGridView, and have the underlying data of that row also be deleted.

I have tried this:

VB.NET:
 'populate the DataGridView (by setting the datasource of the bindingsource that is attached to it)

 PunchEventTableBindingSource.DataSource = TimetrackerDataSet.PunchEventTable.Select(FilterString, SortString)

 'delete the row that is selected (in a "delete" button click event)

 DirectCast(PunchEventTableBindingSource.Current, timetrackerDataSet.PunchEventTableRow).Delete()


Is this the right way to go about this? I am clearly doing something wrong because the row does not get removed from the DataGridView when I click delete.



Thanks,


Dave
 
Erm.. Might be better to do this with datarelations.

Set up your parent table and use it to populate the listbox. When the user picks a row, fill related child rows into the datatable for the datagridview. The bindingsource for the DGV is not bound to the table, but to the datarelation exposed by the parent binding source (see the DW2 link in my sig, section Displaying Related Data).

To remove the current item, just call BindingSource.RemoveCurrent(), though the DGV can handle this intrinsically, all the user has to do is highlight the row and press the Delete key on the keyboard (if AllowUserToDeleteRows is true)

(If you delete the row directly then the bs may not realise that the data changed.. It does in my apps, and I dn't see anything immediately wrong with your code, it's just not the way i'd do it)
 
I got it to work... shouldn't have been this difficult to figure out.

The key was to use the filter() method of the binding source, rather than setting the DataGridView datasource to the select() method of the datatable.

VB.NET:
 PunchEventTableBindingSource.Filter = FilterString
 PunchEventTableBindingSource.Sort = SortString

Then I can delete a row like this:

VB.NET:
DirectCast(PunchEventTableBindingSource.Current, DataRowView).Delete()

and add a row like this:

VB.NET:
  'create a new row
        Dim NewEvent As TTDataSet.PunchEventTableRow = DirectCast(TTDataSet.PunchEventTable.NewRow, TTDataSet.PunchEventTableRow)

        'add all the attributes to the new row
        NewEvent.Associate = AssociateToEdit.Name.ToString
        NewEvent.Reason = cmbReasonAdd.SelectedItem.ToString

        'add the new row to the table
        TTDataSet.PunchEventTable.Rows.Add(NewEvent)

and of course write the changes to the actual database like this:

VB.NET:
PunchEventTableTableAdapter.Update(Me.TTDataSet.PunchEventTable)
 
I got it to work... shouldn't have been this difficult to figure out.
It wasn't; the situation youre describing is what DataRelation is for, you just didn't use it - it basically does the filtering for you

The key was to use the filter() method of the binding source
Or fill the table with only child rows and let the DataRelation filter for you


Then I can delete a row like this:
Or use RemoveCurrent, which does the same thing with less typing :)
 
Back
Top