D
DCraig
I'm having a hard time deleting selected records through a datagrid bound to
a view, VS .Net 03, SQL 2K is the back end although I'm not updating the
database in the code where I'm having a problem.
I am putting the selected rows into an array of datarowviews in the first
loop, that is working fine. When I go through the array to delete the rows
it gets to the last one then I get an index out of range exception, which is
odd because I'm not using an index rather the collection, and I know good
and well there is still another row in the array to delete.
Code is below, it's blowing up on the last record that I know exists in the
array, any help is welcome;
David Craig.
Private Sub deleteTerms(ByVal sender As Object, ByVal e As EventArgs) _
Handles mnuDelete.Click
'Second deletion routine:
' Deletes all article/terms selected in the grid, checks with user and
' displays the terms which will be deleted for this article, if user
approves
' deletes terms from dataset.
'Does not update database.
'Set up some variables
Dim dvw As DataView = dgIndexTerms.DataSource
'Dim tbl As DataTable = dvw.Table
'Dim deleted(tbl.Rows.Count) As DataRow
Dim deleted(dvw.Count) As DataRowView
Dim deleteList As String = ""
Dim i, numDeleted As Integer
Dim termIndex As Integer
numDeleted = 0
termIndex = 1
'Go through grid and get new row for every
'selected record, put that in the deleted row array
For i = 0 To dvw.Count - 1
If dgIndexTerms.IsSelected(i) Then
deleteList &= " " & dvw(i)(2) & " " & dvw(i)(3) & ControlChars.CrLf
Debug.WriteLine("adding to delete array " & dvw(i)(3))
deleted(numDeleted) = dvw(i)
numDeleted += 1
End If
Next
'Confirmation message before delete
Dim ok As DialogResult = _
MessageBox.Show("These terms will be deleted;" & ControlChars.CrLf & _
deleteList & ControlChars.CrLf & "Continue?", _
"Delete warning", MessageBoxButtons.OKCancel)
If ok = DialogResult.OK Then
'User said OK, delete -
'For each row in deleted rows,
'if there's a term id, delete it
Dim drv2 As DataRowView
For Each drv2 In deleted
If Not IsNothing(drv2) Then
Debug.WriteLine("deleting " & drv2(3))
drv2.Delete()
End If
Next
a view, VS .Net 03, SQL 2K is the back end although I'm not updating the
database in the code where I'm having a problem.
I am putting the selected rows into an array of datarowviews in the first
loop, that is working fine. When I go through the array to delete the rows
it gets to the last one then I get an index out of range exception, which is
odd because I'm not using an index rather the collection, and I know good
and well there is still another row in the array to delete.
Code is below, it's blowing up on the last record that I know exists in the
array, any help is welcome;
David Craig.
Private Sub deleteTerms(ByVal sender As Object, ByVal e As EventArgs) _
Handles mnuDelete.Click
'Second deletion routine:
' Deletes all article/terms selected in the grid, checks with user and
' displays the terms which will be deleted for this article, if user
approves
' deletes terms from dataset.
'Does not update database.
'Set up some variables
Dim dvw As DataView = dgIndexTerms.DataSource
'Dim tbl As DataTable = dvw.Table
'Dim deleted(tbl.Rows.Count) As DataRow
Dim deleted(dvw.Count) As DataRowView
Dim deleteList As String = ""
Dim i, numDeleted As Integer
Dim termIndex As Integer
numDeleted = 0
termIndex = 1
'Go through grid and get new row for every
'selected record, put that in the deleted row array
For i = 0 To dvw.Count - 1
If dgIndexTerms.IsSelected(i) Then
deleteList &= " " & dvw(i)(2) & " " & dvw(i)(3) & ControlChars.CrLf
Debug.WriteLine("adding to delete array " & dvw(i)(3))
deleted(numDeleted) = dvw(i)
numDeleted += 1
End If
Next
'Confirmation message before delete
Dim ok As DialogResult = _
MessageBox.Show("These terms will be deleted;" & ControlChars.CrLf & _
deleteList & ControlChars.CrLf & "Continue?", _
"Delete warning", MessageBoxButtons.OKCancel)
If ok = DialogResult.OK Then
'User said OK, delete -
'For each row in deleted rows,
'if there's a term id, delete it
Dim drv2 As DataRowView
For Each drv2 In deleted
If Not IsNothing(drv2) Then
Debug.WriteLine("deleting " & drv2(3))
drv2.Delete()
End If
Next