Vb.net Newbie looping problem

R

Rob W

Greetings,

I have the functionality upon a button press to loop around the rows in the
datagrid view and where the checkbox is true then delete the row (see code
below).

It never deletes all those marked and when I use debug via printing
statements within the loop it randomly seems to skip a row.
I'm not sure if this is because I remove a row from the datagridview
datasource and thus the datagridviewrow count has now changed.
Line tracing shows entering RowValidating/RowValidation but I didn't see
anything strange happening there to impact the looping.

I don't delete via dataAdaptor as the datagridview does not map onto a
single table directly, instead I call bookingDeletion function form a class
which executes a delete SQL statement.

Can anyone please suggest why it does not loop through every single
datagridviewrow in the datagridview?


The looping code is below:-
The full code can be seen here:- http://pastebin.com/m3469f2a


'Loop around each row in datagridview

For Each booking As DataGridViewRow In DataGridView1.Rows

'Is cell value of selected is TRUE and name the same as login

If (booking.Cells("name").Value.Equals(bookersName) AndAlso
booking.Cells("selectColumn").Value = "True") Then

'Remove from database

Dim deleteBooking As New bookingsSession

If deleteBooking.bookingDeletion(cboRoom.SelectedValue,
booking.Cells("bookDate").Value, _

booking.Cells("startTime").Value, booking.Cells("endTime").Value) = True
Then

'Remove booking from datagridview

'DataGridView1.Rows.Remove(booking)

bookingsdata.Tables("bookings").Rows.Remove(DirectCast(booking.DataBoundItem,
DataRowView).Row)

bookingsdata.Tables("bookings").AcceptChanges()

End If

ElseIf (Not booking.Cells("name").Value.Equals(bookersName) AndAlso
booking.Cells("selectColumn").Value = "True") Then

'Highlight which bookings cannot be removed as not made by the user

MessageBox.Show("Warning: You are unable to delete the booking made on " _

& "the " & DirectCast(booking.Cells("bookDate").Value, DateTime).Date _

& " at " & CType(booking.Cells("starttime").Value,
DateTime).ToString("HH:mm") _

& " to " & CType(booking.Cells("endtime").Value, DateTime).ToString("HH:mm")
_

& " as booking was made by someone else.", "Warning: Unable to cancel
booking", _

MessageBoxButtons.OK)

booking.Cells("selectColumn").Value = "False"

End If

Next 'Retrieve next datgridviewrow for expection



Thanks
Rob
 
M

Mythran

Captain Jack said:
It's usually a bad idea to delete from any list or array that you're also
looping through. Just to be safe, I normally build a list of rows to
delete, then delete them in a separate loop, something like this:

Dim DeleteRows As New List(Of DataGridViewRow)
For Each Row As DataGridViewRow In grdMain.Rows
If <ROW SHOULD BE DELETED> Then
DeleteRows.Add(Row)
End If
Next
For Each Row As DataGridViewRow In DeleteRows
grdMain.Rows.Remove(Row)
Next
DeleteRows.Clear()

There may be better ways to do that, but that seems pretty safe, and I
like safe. :)

If you reverse the direction the loop is iterating through, it would be fine
to delete form the list. This involves not using For Each, but rather just
For.

For i As Integer = grdMain.Rows.Count - 1 To 0 Step -1
If {check here}
grdMain.Rows.Remove(i)
End If
Next I

HTH ;)

Mythran
 
R

Rob W

Thanks for the replies, It's now dawned on me where the problem lies.

My datagridview is created MANUALLY and then assigned to a datatable with
all but TWO of the column are assigned values via .DataPropertyName

When im removing the datarow from the datatable updating the datagridview it
removes the datagridview checkbox column values which isn't bound to the
datatable, what options do I have to avoid this?

The most striking one would to be include the selection checkbox/boolean
into the datatable, any other options?

Thanks
Rob
 

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