Strange ComboBox Behavior

  • Thread starter Thread starter Greg Smith
  • Start date Start date
G

Greg Smith

I am experiencing strange behavior working with multiple
ComboBoxes on a form.

I have several ComboBoxes that I configure in code the
way I have in the past (only a single ComboxBox on the
form). Whatever I configure the last ComboxBox to, it
changes all the previous ComboBoxes to the same displayed
value.

Here is the code:

private void Form1_Load(object sender, System.EventArgs e)
{
da.Fill(ds.tblQuestions);
daTypes.Fill(ds.tblQuestionTypes);

BindCB1();
BindCB2();
}

private void BindCB1()
{
comboBox1.DataSource = ds;
comboBox1.DisplayMember
= "tblQuestionTypes.QuestionTypeDesc";
comboBox1.ValueMember
= "tblQuestionTypes.QuestionTypeNumber";
comboBox1.DataBindings.Add("SelectedValue",
ds, "tblQuestions.Question01Type");
comboBox1.DropDownStyle.Equals("DropDownList");
}

private void BindCB2()
{
comboBox2.DataSource = ds;
comboBox2.DisplayMember
= "tblQuestionTypes.QuestionTypeDesc";
comboBox2.ValueMember
= "tblQuestionTypes.QuestionTypeNumber";
comboBox2.DataBindings.Add("SelectedValue",
ds, "tblQuestions.Question02Type");
comboBox2.DropDownStyle.Equals("DropDownList");
}

Any help is greatly appreciated.
 
Try using two copies of ds, ie ds1 and ds2. But do not
say ds2 = ds1; say ds2 = new (ds1);
See if this helps :)
 
I am experiencing strange behavior working with multiple
ComboBoxes on a form.

I have several ComboBoxes that I configure in code the
way I have in the past (only a single ComboxBox on the
form). Whatever I configure the last ComboxBox to, it
changes all the previous ComboBoxes to the same displayed
value.

If I understand your problem correctly, what's happening is that all the
combo boxes on the page are binding to the same BindingContext. So when
you change the current row in your default BindingContext, all the combo
boxes are adjusting themselves to reflect the current row. The simple
fix is to create a new Binding Context for one of the boxes, like...

private void BindCB2()
{
comboBox2.BindingContext = new BindingContext();
comboBox2.DataSource = ds;
comboBox2.DisplayMember = "tblQuestionTypes.QuestionTypeDesc";
comboBox2.ValueMember = "tblQuestionTypes.QuestionTypeNumber";
comboBox2.DataBindings.Add("SelectedValue", ds,
"tblQuestions.Question02Type");
comboBox2.DropDownStyle.Equals("DropDownList");
}

BTW, I'm pretty sure the line:

comboBox2.DropDownStyle.Equals("DropDownList");

doesn't do anything useful. All "Equals" does is compare items and
return a boolean. Since you aren't doing anything with the return
value, this is essentially a no-op (or possibly I'm reading this wrong,
the original indenting was a little funny here).
 
Hi Greg,
Do you mean , after you bind the ComboBox2, then any selection changes on
ComboBox2 will also change the other ComboBox to the according value?
If yes, does our community member's solutions solve your problem?
If you still have problem on this issue, please be free to post it on the
group!
I'll follow-up with you.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: David <[email protected]>
| Subject: Re: Strange ComboBox Behavior
| References: <[email protected]>
| Message-ID: <[email protected]>
| User-Agent: slrn/0.9.7.4 (Linux)
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Date: Thu, 23 Oct 2003 12:44:48 -0700
| NNTP-Posting-Host: 129-138.175-24.bham.rr.com 24.175.138.129
| Lines: 1
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:55123
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| > I am experiencing strange behavior working with multiple
| > ComboBoxes on a form.
| >
| > I have several ComboBoxes that I configure in code the
| > way I have in the past (only a single ComboxBox on the
| > form). Whatever I configure the last ComboxBox to, it
| > changes all the previous ComboBoxes to the same displayed
| > value.
|
| If I understand your problem correctly, what's happening is that all the
| combo boxes on the page are binding to the same BindingContext. So when
| you change the current row in your default BindingContext, all the combo
| boxes are adjusting themselves to reflect the current row. The simple
| fix is to create a new Binding Context for one of the boxes, like...
|
| private void BindCB2()
| {
| comboBox2.BindingContext = new BindingContext();
| comboBox2.DataSource = ds;
| comboBox2.DisplayMember = "tblQuestionTypes.QuestionTypeDesc";
| comboBox2.ValueMember = "tblQuestionTypes.QuestionTypeNumber";
| comboBox2.DataBindings.Add("SelectedValue", ds,
| "tblQuestions.Question02Type");
| comboBox2.DropDownStyle.Equals("DropDownList");
| }
|
| BTW, I'm pretty sure the line:
|
| comboBox2.DropDownStyle.Equals("DropDownList");
|
| doesn't do anything useful. All "Equals" does is compare items and
| return a boolean. Since you aren't doing anything with the return
| value, this is essentially a no-op (or possibly I'm reading this wrong,
| the original indenting was a little funny here).
|
| --
| David
| dfoster at
| hotpop dot com
|
 
Thank a million David. That was the fix. :->

David said:
If I understand your problem correctly, what's happening is that all the
combo boxes on the page are binding to the same BindingContext. So when
you change the current row in your default BindingContext, all the combo
boxes are adjusting themselves to reflect the current row. The simple
fix is to create a new Binding Context for one of the boxes, like...

private void BindCB2()
{
comboBox2.BindingContext = new BindingContext();
comboBox2.DataSource = ds;
comboBox2.DisplayMember = "tblQuestionTypes.QuestionTypeDesc";
comboBox2.ValueMember = "tblQuestionTypes.QuestionTypeNumber";
comboBox2.DataBindings.Add("SelectedValue", ds,
"tblQuestions.Question02Type");
comboBox2.DropDownStyle.Equals("DropDownList");
}

BTW, I'm pretty sure the line:

comboBox2.DropDownStyle.Equals("DropDownList");

doesn't do anything useful. All "Equals" does is compare items and
return a boolean. Since you aren't doing anything with the return
value, this is essentially a no-op (or possibly I'm reading this wrong,
the original indenting was a little funny here).
 
Back
Top