Problems deleting through datagrid/view

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
 
R

Rajesh Patel

It looks a long way. but, try to delete bottom to top. It may solve your
problem.

Regards,

Rajesh Patel
 
C

Cor Ligthert

Hi,

I did not look extensively to your code, please when you send a problem next
time and it is the first message, show only that part what is important for
the message (just a row of ten, when it becomes real difficult someone will
ask you for sure for more details).

In my opinion, mostly your problem comes because the direction of the loop
you can avoid that by

\\\
for i as integer = dv.count-1 to 0 step -1
next
///

I hope this helps?

Cor
 
C

Cor Ligthert

Rajesh,

I had anserwered when I saw that you had already answered it almost the
same,

There was no other reason,

Cor

"Rajesh Patel"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top