Not deleting the right row

doraemon

Freshman
Joined
Oct 3, 2003
Messages
36
I have 2 datagrids, one parent and one child. When I delete a record in the child's table, the wrong record got deleted. I tried stepping through the code and I think it has to do with the rowfilter I have on the datatable of the child's table. The rowfilter is there so that when the user clicks on the parent's record, the child's datagrid will show the filtered corresponding records. But, when I tried to delete a child (let's say the first record), it would delete the first record of the table. How can I get this resolved?
 
Here's a section of the codes:

On the click of the parent's grid:

dataset.Tables("Table1").Defaultview.rowfilter = myfilter

.
.
.

Then, on the delete button:

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim aTable As DataTable = dsTASKS.Tables("table2")

If GetSelectedRows(ChildDataGrid).Count > 0 Then
Dim o As Object
For Each o In GetSelectedRows(ChildDataGrid)
Dim aRow As DataRow = aTable.Rows(CInt(o))
aRow.Delete()
Next o
Try
CType(ChildDataGrid.DataSource, DataTable).DefaultView.AllowDelete = True
daChild.Update(dataset, "table2")
dataset.AcceptChanges()
CType(ChildDataGrid.DataSource, DataTable).DefaultView.AllowDelete = False
Catch ex As SqlException
dataset.RejectChanges()
MessageBox.Show(ex.Message)
End Try
Else
MessageBox.Show("No Rows selected for Deletion.")
End If
End Sub

Public Function GetSelectedRows(ByVal dg As DataGrid) As ArrayList
Dim al As New ArrayList

Dim dv As DataView = CType(_currencyManager.List, DataView)
Dim i As Integer

While i < dv.Count
If dg.IsSelected(i) Then
al.Add(i)
End If
i = i + 1
End While
Return al
End Function

----
It would work if I did not have the rowfilter on the click of the parent's datagrid. But I need the rowfilter to show only the corresponding records in the child's datagrid.

Also, I did not allow deleting of rows by hitting the delete key after selecting rows. That's why I had the allowdelete set to False and only made it true when the button was clicked.

How can I get this to work?
 
Back
Top