Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Vb.net Newbie looping problem

7 views
Skip to first unread message

Rob W

unread,
Nov 20, 2009, 2:34:24 PM11/20/09
to
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


Captain Jack

unread,
Nov 20, 2009, 2:50:33 PM11/20/09
to
"Rob W" <rob...@o2.co.uk> wrote in message
news:OT3cBiha...@TK2MSFTNGP06.phx.gbl...

> 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'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. :-)

--
Jack


Mythran

unread,
Nov 20, 2009, 4:15:50 PM11/20/09
to

"Captain Jack" <Captain...@comcast.net> wrote in message
news:reednYqAsdwob5vW...@giganews.com...

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

Captain Jack

unread,
Nov 20, 2009, 5:00:49 PM11/20/09
to
"Mythran" <Myt...@community.nospam> wrote in message
news:A8A396FB-2EBD-4EB1...@microsoft.com...

I think I'm poisoned by having problems with that in another programming
language, long ago, and I'm something of a slave to "For Each".

I think thie the DataGridView, you'd have to do
grdMain.Rows.Remove(grdMain.Rows(i)). IIRC, the Remove method only takes a
row instance as an argument.

--
Jack


Rob W

unread,
Nov 21, 2009, 11:44:00 AM11/21/09
to
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

"Captain Jack" <Captain...@comcast.net> wrote in message

news:PaGdnXMk_vuhjJrW...@giganews.com...

Captain Jack

unread,
Nov 23, 2009, 10:25:13 AM11/23/09
to
"Rob W" <rob...@o2.co.uk> wrote in message
news:%23sVTgns...@TK2MSFTNGP05.phx.gbl...

> 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

I'm not sure about that one... I've only ever use the DataGridView control
as either completely bound to a DataTable, or completely manually creating
the rows. Lately, I've stuck to manually creating the rows, usually from a
SqlDataReader. I find that it loads a tiny bit faster and I get a little bit
better memory usage that way (for some reason, DataTable seems to take a
while to get garbage collected) and then I've got more freedom to work with
individual rows in the grid. That works pretty well for me, because I don't
have any code that lets users do edits in the grid itself; when a user needs
to edit a row, I pop up a window with editing controls, then I change the
row when the window exits.

I do have one situation where I let the user click multiple check boxes in a
grid, then I do an update on those rows and remove them. I use the technique
I described above, though; I scan the grid, copy the rows to be updated to a
temporary list, do the update, use the temp list to remove the rows, then
clear the list out.

--
Jack


0 new messages