Question Removing items from a control that has a DataSource attached.

Benrie

New member
Joined
Dec 18, 2014
Messages
3
Programming Experience
10+
Hello,

Looking for a bit of help and maybe one of you has a solution for my dilemma. :)

I have some dynamically created combobox controls and these have been assigned a data source along with a display member and a value member. I am able to add new items to the data source but haven't figured out a way to remove an item from the data source, which in turn removes it from the combobox. I can blank out the display member but that's it.

Here is a sample of what I've done so far:

Dim newCombo As ComboBox = Question
RemoveHandler newCombo.SelectedValueChanged, AddressOf RunRule
Dim newDataSource = newCombo.DataSource
Dim newDisplayMember = newCombo.DisplayMember
Dim newValueMember = newCombo.ValueMember
newCombo.DataSource = Nothing
For Each item In newDataSource
If item.AnswerName = ctlResult Then
item.AnswerName = String.Empty
End If
Next
newCombo.DataSource = newDataSource
newCombo.DisplayMember = newDisplayMember
newCombo.ValueMember = newValueMember
AddHandler newCombo.SelectedValueChanged, AddressOf RunRule

Thanks for any help provided. :)
 
Exactly what type is the data source? The fact that it is bound to a control is irrelevant for how you use it. If it's a generic List then you call its Remove method, plain and simple.
 
Exactly what type is the data source? The fact that it is bound to a control is irrelevant for how you use it. If it's a generic List then you call its Remove method, plain and simple.

Thanks for your reply.

Unfortunately I tried the Remove method and it didn't work. When I tried it on the combobox itself (newCombo.Items.Remove("ItemToRemove") it didn't error but also didn't remove the item.
When I tried it on the data source (item.Remove) it failed.

The data source is a list of items from a class. The class has only two properties.
 
newCombo.Items.Remove
That is not possible, since the bound combobox doesn't have any items (it's the data source that has the items).
When I tried it on the data source (item.Remove) it failed.
You have to be more specific, that doesn't make sense.
The data source is a list of items from a class
If you mean a List(Of T) where T is item type, then that List has a Remove method.
 
Thanks for your reply.

Unfortunately I tried the Remove method and it didn't work. When I tried it on the combobox itself (newCombo.Items.Remove("ItemToRemove") it didn't error but also didn't remove the item.
When I tried it on the data source (item.Remove) it failed.

The data source is a list of items from a class. The class has only two properties.

Unfortunately, that is a very nondescript description. What type EXACTLY is the data source? Of course it's a list of some type. If it wasn't then it couldn't be a data source. What is it EXACTLY? Also, what does "fail" mean EXACTLY? Was an exception thrown? If so what was the error message? If not then I can only assume that the item you tried to remove wasn't actually in the list to start with.
 
That is not possible, since the bound combobox doesn't have any items (it's the data source that has the items).

You have to be more specific, that doesn't make sense.

If you mean a List(Of T) where T is item type, then that List has a Remove method.


I figured as much when trying to remove it from the combobox.

So the data source is a List(Of T) that was assigned to the combobox. I can add items to the data source successfully and that does affect the combobox. You did give me an idea so I tested it and I think it works. Here is what I did:
For Each item In DataSource
If item.AnswerName = ctlResult Then
DataSource.Remove(item)
Exit For
End If
Next

It did not error on me so I believe this took care of it. The count of elements in the data source list decreased by 1.

I knew it had to be simple. Simple get's me everytime. :)

Thanks for the clue! :)
 
I figured as much when trying to remove it from the combobox.

So the data source is a List(Of T) that was assigned to the combobox. I can add items to the data source successfully and that does affect the combobox. You did give me an idea so I tested it and I think it works. Here is what I did:
For Each item In DataSource
If item.AnswerName = ctlResult Then
DataSource.Remove(item)
Exit For
End If
Next

It did not error on me so I believe this took care of it. The count of elements in the data source list decreased by 1.

I knew it had to be simple. Simple get's me everytime. :)

Thanks for the clue! :)

That seems to suggest that you were probably trying to remove an item that didn't actually exist in the collection. The method actually returns True or False to indicate whether the specified item was found and removed. There's nothing specifically wrong with using a loop as you are but you do have other options. The RemoveAll method removes all items that match specific criteria, so you could do this instead:
DataSource.RemoveAll(Function(i) i.AnswerName = ctlResult)
That will work if there are zero, one or more matching items. The difference there is that it checks all items, assuming that there can be multiple matches. If it's a large collection and there can be no more than one match then it might actually be less efficient. If, as I suspect, it's only a small collection then the difference will be negligible. You could even write your own extension method that only removed the first matching item and was therefore more efficient:
Imports System.Runtime.CompilerServices

Public Module ListExtensions

    ''' <summary>
    ''' Removes the first matching item from a list.
    ''' </summary>
    ''' <typeparam name="T">
    ''' The type of the items in the list.
    ''' </typeparam>
    ''' <param name="source">
    ''' The list to remove the item from.
    ''' </param>
    ''' <param name="predicate">
    ''' The criteria that the item satisfies.
    ''' </param>
    ''' <returns>
    ''' <b>True</b> if a matching item is found and removed; otherwise, <b>False</b>.
    ''' </returns>
    <Extension>
    Public Function RemoveFirst(Of T)(source As IList(Of T), predicate As Func(Of T, Boolean)) As Boolean
        'Find the first item that matches the criteria if there is one.
        Dim item = source.FirstOrDefault(predicate)

        Dim result = item IsNot Nothing

        'If a matching item was found, remove it from the list.
        If result Then
            source.Remove(item)
        End If

        Return result
    End Function

End Module
and then call it in much the same way:
DataSource.RemoveFirst(Function(i) i.AnswerName = ctlResult)
 
Back
Top