lune
lune

Reputation: 185

Multiple Comboboxes with a list of objects

I have a 3 ComboBoxes in a Form, a list of objects. I need to bind the the comboboxes with 3 different members of the class from the list. (C# 3.0, .NET 3.5) I am currently doing this

Title_Combo.DataSource = ListContaining.GroupBy(item => item.Title).Where(item => !item.Key.Equals(string.Empty)).ToList();

Title_Combo.DisplayMember = "Key";

Where ListContaining is a subset of the main list of objects.Every time an item is selected in any one of those comboboxes the ListContaining is populated based on selected value from the main list of objects like and all the comboboxes are reloaded.

ListContaining = ListFiles.Where(item => item.GetType().GetProperty(name).GetValue(item, null).Equals(int.Parse(Sender.SelectedItem.ToString()))).ToList();

It loads perfectly but the next selection of the comboboxes throws a NullReference Exception.

Is this due to the fact that the List ListContaining is being rewritten or something, I can figure out.

and is there a better way to handle the 3 comboboxes from the list.

Your help is appreciated.

EDITED: I have given up debugging this. But can anyone suggest a way to bind 3 comboboxes with a single list of objects with 3 different properties. And the controls update on index change.

Upvotes: 1

Views: 795

Answers (3)

lune
lune

Reputation: 185

Well, I got the answer.
You can use a subset of objects to bind a control, that was not the cause of the problem.
And I am able to handle multiple comboboxes in the way described.

Upvotes: 0

someone
someone

Reputation: 381

Why don't you use additional lists that stores the values of comboboxes?

So for each combobox, you have a list of strings. You can also store all these different lists of strings in another structure, like dictionary.

Perhaps this will cause more lines of code and additional memory use but in return you get a more easily manageable code.

Upvotes: 1

Myra
Myra

Reputation: 3656

This problem may become if list type of your second combobox is DropDown not DropDownList, normally the same error in that exception you mention returns.Please check your controls.

For a second thought , If your comboboxes are related each other ,as follows:

One to many relation

  • ComboBox:CompanyGroup
  • ComboBox:Company
  • ComboBox:Person

-->If one changes from above , below is triggered. You case is like :

Many to Many relation

  • ComboBox:Tags
  • ComboBox:Questions

--> If question changes it triggers its own tags and If tags changes it triggers for only which tag the question has.

For this purpose only,you should search in entire collection each time your combobox item changes.Because as I understand from your question,one choice triggers another choice.

Upvotes: 1

Related Questions