Remove Item in NVL gets Error

Remove Item in NVL gets Error

Old forum URL: forums.lhotka.net/forums/t/3721.aspx


griff posted on Tuesday, October 16, 2007

Hi

using a NVL as  the datasource for a listbox and want to remove an item from the list.

I have added a sub.....

Public Overloads Sub Remove(ByVal clientID As Int32)

For Each res As NameValuePair In Me

If res.Key = clientID Then

Me.remove(res)

Exit For

End If

Next

End Sub

This throws the following error...

"Remove is an invalid operation"

Can anyone shed light on this, thanks

Richard

 

 

DavidDilworth replied on Tuesday, October 16, 2007

The NVL base class extends ReadOnlyBindingList - the list is always ReadOnly.

You need to make it editable while you do your change.  The following code is an example:

// Stop raising list events while changing the list
RaiseListChangedEvents = false;

// Make the list not ReadOnly (so it can be changed)
IsReadOnly = false;

// Do some changes here....
<snip>;

// Make the list ReadOnly again
IsReadOnly = true;

// Start raising list events again
RaiseListChangedEvents = true;

griff replied on Tuesday, October 16, 2007

Thanks that sorted it.

Richard

griff replied on Tuesday, October 16, 2007

Hi

further to this....removing the item from the list works fine and I can now add into the list in a similar fashion. However the newly added item is added to the bottom of the lstBox/NVL - how do you apply sorting to the NVL?

Richard

 

DavidDilworth replied on Tuesday, October 16, 2007

Have you tried using the Insert(index, item) method to put the item into the right place in the list?

griff replied on Tuesday, October 16, 2007

No, but how would I know what the value of 'index' was?

I'm looking into using a sorted list to handle this

Richard

 

DavidDilworth replied on Wednesday, October 17, 2007

You'd have to work out what the index was for yourself.

Don't forget that the main purpose behind the NVL is to populate drop-down list style controls from data in a DB table.  Usually all the sorting is done when you get the data from the DB.

It sounds like you've got a slightly different requirement here.  So that means you need to write some slightly different code to handle your specific case.

The best solution will always be to sort the data before you start building the NVL though.

HTH.

Copyright (c) Marimer LLC