DataBinding to ComboBox Not Saving

  • Thread starter Thread starter Joe Keller
  • Start date Start date
J

Joe Keller

Hello,

I have a combo box that gets it lookup values from table A but is bound to
table B. When I pull up my form and navigate through the records, the
records are read and displayed correctly in all of my fields, including the
combo box. However, when I change the value of what is shown in the combo
box, the changed value does not "stick". If I navigate away and then back
to the record I just changed the combo box value has reverted back to its
old value. The text box controls on my form properly keep their changed
values as one would expect.

Could someone tell me if I'm doing something wrong here? I'm not getting
any errors at compile or run-time. The only thing I'm doing out of the
"norm" is actually saving the text value of the combo box to the main table
from which it is bound to (the main table expects a text value, not an
integer).

Here's the relevant snippet of code:

-----------------------------------------------------
comboBox1.Visible=false;
DataTable dt = new DataTable("tblCategoryType");
DataTable dt2 = new DataTable("tblInventoryItem");

myCommand.CommandText = "SELECT sCategoryTypeID FROM tblCategoryType";

SqlCeDataAdapter da;
da = new SqlCeDataAdapter(myCommand);
da.Fill(dt);

myCommand.CommandText = "SELECT * FROM tblInventoryItem";

SqlCeDataAdapter da2;
da2 = new SqlCeDataAdapter(myCommand);
da2.Fill(dt2);

comboBox1.DataSource=dt;
comboBox1.DisplayMember="sCategoryTypeID";
comboBox1.ValueMember="sCategoryTypeID";
comboBox1.DataBindings.Add("Text", dt2, "sCategoryTypeID");

comboBox1.Visible=true;
-----------------------------------------------------


The table DDL's look like the following:
-----------------------------------------------------
/* tblCategoryType*/
CREATE TABLE tblCategoryType (sCategoryTypeID nVarChar(25) NOT NULL PRIMARY
KEY)

/* tblInventoryItem */
CREATE TABLE tblInventoryItem(lInventoryItemID int IDENTITY PRIMARY KEY,
sItemName nVarChar(30) NOT NULL, sItemDescription nVarChar(25) NULL,
sCategoryTypeID nVarChar(25) NOT NULL REFERENCES tblCategoryType
(sCategoryTypeID))

-----------------------------------------------------

Any help is appreciated!

Thanks,

Joe
 
Try replacing:
comboBox1.DataBindings.Add("Text", dt2, "sCategoryTypeID");
with:
comboBox1.DataBindings.Add("SelectedValue",
dt2, "sCategoryTypeID");

Regards,
Mikael
 
Mikael,

Thanks - that solved it!

However, I've been looking high and low for the answer to this - how would
one know to use "SelectedValue" vs. "Text"?

Joe
 
The ComboBox does not fire a TextChanged event when a different item is
selected. This is a known bug which causes the DataBinding to not work
correctly when the Text property on ComboBox is used as a DataBinding
target. The appropriate work-around is to use the SelectedValue property.

David Wrighton

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| NNTP-Posting-Date: Sun, 12 Oct 2003 10:01:04 -0500
| From: "Joe Keller" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| References: <[email protected]>
<[email protected]>
| Subject: Re: DataBinding to ComboBox Not Saving
| Date: Sun, 12 Oct 2003 11:00:59 -0400
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Lines: 108
| NNTP-Posting-Host: 68.60.102.111
| X-Trace:
sv3-vIUkYRu0SI52+4LDV5wQRsbe7iZ9PmQThtEcYkmeowmTX0iLh7zxR5V1MO2kcNt1SEawqOn6
BwDwnDw!rJ2gbvSMRhfAm6cWui0oMSBFziBSPoxmrjV5kOqJCb1aP60/gKHZw6dDlveIKQ==
| X-Complaints-To: (e-mail address removed)
| X-DMCA-Complaints-To: (e-mail address removed)
| X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
| X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your
complaint properly
| X-Postfilter: 1.1
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!newsfee
d01.sul.t-online.de!t-online.de!newspeer1-gui.server.ntli.net!ntli.net!peer0
1.cox.net!cox.net!border3.nntp.aus1.giganews.com!intern1.nntp.aus1.giganews.
com!nntp.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:35765
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Mikael,
|
| Thanks - that solved it!
|
| However, I've been looking high and low for the answer to this - how would
| one know to use "SelectedValue" vs. "Text"?
|
| Joe
|
| | > Try replacing:
| > comboBox1.DataBindings.Add("Text", dt2, "sCategoryTypeID");
| > with:
| > comboBox1.DataBindings.Add("SelectedValue",
| > dt2, "sCategoryTypeID");
| >
| > Regards,
| > Mikael
| >
| > >-----Original Message-----
| > >Hello,
| > >
| > >I have a combo box that gets it lookup values from table
| > A but is bound to
| > >table B. When I pull up my form and navigate through the
| > records, the
| > >records are read and displayed correctly in all of my
| > fields, including the
| > >combo box. However, when I change the value of what is
| > shown in the combo
| > >box, the changed value does not "stick". If I navigate
| > away and then back
| > >to the record I just changed the combo box value has
| > reverted back to its
| > >old value. The text box controls on my form properly
| > keep their changed
| > >values as one would expect.
| > >
| > >Could someone tell me if I'm doing something wrong here?
| > I'm not getting
| > >any errors at compile or run-time. The only thing I'm
| > doing out of the
| > >"norm" is actually saving the text value of the combo box
| > to the main table
| > >from which it is bound to (the main table expects a text
| > value, not an
| > >integer).
| > >
| > >Here's the relevant snippet of code:
| > >
| > >-----------------------------------------------------
| > >comboBox1.Visible=false;
| > >DataTable dt = new DataTable("tblCategoryType");
| > >DataTable dt2 = new DataTable("tblInventoryItem");
| > >
| > >myCommand.CommandText = "SELECT sCategoryTypeID FROM
| > tblCategoryType";
| > >
| > >SqlCeDataAdapter da;
| > >da = new SqlCeDataAdapter(myCommand);
| > >da.Fill(dt);
| > >
| > >myCommand.CommandText = "SELECT * FROM tblInventoryItem";
| > >
| > >SqlCeDataAdapter da2;
| > >da2 = new SqlCeDataAdapter(myCommand);
| > >da2.Fill(dt2);
| > >
| > >comboBox1.DataSource=dt;
| > >comboBox1.DisplayMember="sCategoryTypeID";
| > >comboBox1.ValueMember="sCategoryTypeID";
| > >comboBox1.DataBindings.Add("Text",
| > dt2, "sCategoryTypeID");
| > >
| > >comboBox1.Visible=true;
| > >-----------------------------------------------------
| > >
| > >
| > >The table DDL's look like the following:
| > >-----------------------------------------------------
| > >/* tblCategoryType*/
| > >CREATE TABLE tblCategoryType (sCategoryTypeID nVarChar
| > (25) NOT NULL PRIMARY
| > >KEY)
| > >
| > >/* tblInventoryItem */
| > >CREATE TABLE tblInventoryItem(lInventoryItemID int
| > IDENTITY PRIMARY KEY,
| > >sItemName nVarChar(30) NOT NULL, sItemDescription nVarChar
| > (25) NULL,
| > >sCategoryTypeID nVarChar(25) NOT NULL REFERENCES
| > tblCategoryType
| > >(sCategoryTypeID))
| > >
| > >-----------------------------------------------------
| > >
| > >Any help is appreciated!
| > >
| > >Thanks,
| > >
| > >Joe
| > >
| > >
| > >.
| > >
|
|
|
 
Back
Top