0

I am having a difficulty adding an item to my listbox. I want to add an item at the beginning of the list box to be my 'default' item, however I will also be adding a list of items from a List using the .DataSource ... and for some reason the app is crashing whenever I try to add the items from the List and the default item at the same time. I am trying to add the items by using:

`productList.DataSource = salesManager.Products;
productList.DisplayMember = "IdAndName";
productList.ValueMember = "Id";
productList.Items.Insert(0, "ALL");`

but for some reason VS will not let me. I have also found this method and tried to apply it as well:

public void AddListLine(string lineIn)
    {
        productList.Items.Insert(0, "ALL");
        ((CurrencyManager)productList.BindingContext[productList]).Refresh();
    }

However it is not working as well. Any idea please? Thanks!

1 Answer 1

2

The reason it isn't working is because you are attempting to add an object of type String where the rest are (I assume) of type Product or something similar. The runtime attemps to access the property IdAndName to display and the property Id for the display and value properties of the new list item and they do not exist.

Consider adding some kind of "blank" Product object instead.

public void AddListLine(string lineIn)
    {
        productList.Items.Insert(0, new Product { Id = "ALL", IdAndName = "ALL" });
        ((CurrencyManager)productList.BindingContext[productList]).Refresh();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.