views:

234

answers:

3

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.

+1  A: 

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.

Myra
Yes mine is dropdown style, and it is not causing the problem as a the value selected is checked for null values.Yes, mine is many to many relation and hence i group and search the list.I am concerned that the list changes everytime i select so, does that cause the problem.
lune
+1  A: 

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.

someone
Yes, that can be done but, the values can be edited and hence there would be need for synchronizing with other lists.This would be the solution if there is no better solution.I am not sure this can be done with a dictionary.
lune
Agreed. This is why I said additional lines of code. Anyhow, I had a similar problem and this was my solution. Rather less efficient as it is but having list of strings also provides me some extra functionality. Also, personally I prefer multiple of smaller sized lines of code instead of smartly written but a very long 1 line of code.
someone
A: 

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.

lune