views:

364

answers:

5

I am dynamically creating a combobox like this:

public Control GenerateList(Question question)
{
    // Get a list with answer possibilities
    List<QuestionAnswer> answers = question.GetAnswers();

    // Get a collection of given answers
    Collection<QuestionnaireAnswer> givenAnswers = question.GetFilledAnswers();

    ComboBox cmb = new ComboBox();
    cmb.Name = "cmb";
    cmb.DataSource = answers;
    cmb.DisplayMember = "Answer";
    cmb.ValueMember = "Id";

    // Check an answer is given to the question
    if (givenAnswers != null && givenAnswers.Count > 0)
    {
        cmb.SelectedValue = givenAnswers[0].AnswerId;

    }

    cmb.DropDownStyle = ComboBoxStyle.DropDownList;
    cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
    cmb.Leave += new EventHandler(cmb_Leave);

    return cmb;
}

The problem is,when executing cmb.SelectedValue = givenAnswers[0].AnswerId; cmb.SelectedValue is always null.

When debugging and I explore answers (the datasource) I see that Id (ValueMember) is exactle the same as AnswerId (in the if statement). Both have the same type (long) and the same value, but SelectedValue stays null.

Is there something I don't see?

EDIT:

It looks like the combobox stays empty. When I replace cmb.SelectedValue = givenAnswers[0].AnswerId; with cmb.SelectedIndex = 0; I get an ArgumentOutOfRangeException. This while the answers collections count is 2. So the datasource isn't null... Very strenge huh?

Solution:

The SelectedValue, SelectedIndex, SelectedItem properties can't be set until the control is added to the form. After the control is added to the form, the selectedValue, -Index and -Item properties can be set.

A: 

Make sure QuestionAnswer has public accessors corresponding (same name) to the Display/Value Members you use.

thelost
QuestionAnswer is a subclass. The base class contains the Id and Answer properties, which are both public with a getter and setter.
Martijn
A: 

I met this weird issue before, at last I gave up and used another way:

cmb.Items.FindByValue(givenAnswers[0].AnswerId).Selected = true;

It worked fine...Hope you good luck!

Danny Chen
I don't have the method FindByValue. Maybe it's because I'm using winforms?
Martijn
WinForm Combo must also have such a method like FindByValue of the web control(maybe a different name). Try it.
Danny Chen
+1  A: 

Are you looking at the same property?

 cmb.ValueMember = "Id"; 
 ..
 cmb.SelectedValue = givenAnswers[0].AnswerId; 

You're refering to another ValueMember then the Id you're posting into the SelectedValue. Besides that you might want to try to set your Display- and Value-member before databinding. It's faster.

riffnl
The datasource contains a different class. givenAnswers contains another class, but the value of AnswerId and Id is the same.
Martijn
Same types or same values?
riffnl
A: 

Are QuestionAnswer.Answer and QuestionAnswer.Id public properties?

Jason
Yes, see my comment @TheLost
Martijn
A: 

Solution:

The SelectedValue, SelectedIndex, SelectedItem properties can't be set until the control is added to the form. After the control is added to the form, the selectedValue, -Index and -Item properties can be set.

Martijn