newbie: 2 databindings questions

D

Dan

I'm relatively new to forms databinding, anyway I have experimented a bit
and I have a couple of questions:

1) often, the databinding mechanism seems not to realize that changes have
been made to the bound control value(s). For instance, if I bind a textbox
or a numeric up-down control to a data source (a datatable row's field), the
display is always correct, but if I change some letters in the textbox or a
value in the up-down control, WITHOUT moving around to other controls in the
form for a while, the call to EndCurrentEdit *does not reflect* the new
value in the controls. I call EndCurrentEdit like:

BindingContext[myDataSet, "tableName"].EndCurrentEdit();

for each table in my dataset whose field(s) are bound to any control in the
form. The correct value is reflected only when I move the focus to other
controls in the form, if I just change the control contents and then click
on my UPDATE button, the underlying datarow state remains UNCHANGED. Is this
my fault, or it's just a buggy behaviour of databinding?


2) what's the correct way for binding to a combobox? Let's say I have a form
which displays some 'lookup' data in a combo: these data are bound to a
parent table (e.g. Countries), and the selected item primary key is linked
to a corresponding foreign key in a child table (e.g. Customer): the
database looks like:

TCustomer: customerID, countryID (the foreign key), ...
TCountry: countryID (the primary key), country

The countries combo will be bound to my (typed) dataset's table TCountry as
follows:

myCombo.DataBindings.Add(new Binding("SelectedValue", myDataSet,
"TCountry.countryID"));
myCombo.DataSource = myDataSet.TCountry;
myCombo.DisplayMember = "country";
myCombo.ValueMember = "countryID";

Now, when loading customer's data into the form, I'll have to "manually"
select the correct country in the lookup combo:

MyDataSet.TCountryRow row =
myDataSet.TCountry.FindBycountryID(row.countryID);
myCombo.SelectedIndex = myCombo.FindStringExact(row.country);

And when updating the database, before calling EndCurrentEdit() and updating
via the data adapter I'll have to do the inverse:

theCustomerRow.countryID = (int)_myCombo.SelectedValue;

Is this OK, or is there a better way for doing it?
 
K

Kevin Yu [MSFT]

Hi Dan,

1. You have to call EndCurrentEdit() to let the dataset know that you have
end editing the current datarow, so that the dataset will set the RowState
accordingly.

2. Based on my experience, there doesn't seem to be a way to achieve this
using data binding. I'm afraid you have to write code to do this.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Dan" <[email protected]>
| Subject: newbie: 2 databindings questions
| Date: Sat, 15 Nov 2003 19:02:46 +0100
| Lines: 53
| 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]>
| Newsgroups: microsoft.public.dotnet.framework.adonet
| NNTP-Posting-Host: ppp-217-133-159-3.cust-adsl.tiscali.it 217.133.159.3
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:66427
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| I'm relatively new to forms databinding, anyway I have experimented a bit
| and I have a couple of questions:
|
| 1) often, the databinding mechanism seems not to realize that changes have
| been made to the bound control value(s). For instance, if I bind a textbox
| or a numeric up-down control to a data source (a datatable row's field),
the
| display is always correct, but if I change some letters in the textbox or
a
| value in the up-down control, WITHOUT moving around to other controls in
the
| form for a while, the call to EndCurrentEdit *does not reflect* the new
| value in the controls. I call EndCurrentEdit like:
|
| BindingContext[myDataSet, "tableName"].EndCurrentEdit();
|
| for each table in my dataset whose field(s) are bound to any control in
the
| form. The correct value is reflected only when I move the focus to other
| controls in the form, if I just change the control contents and then click
| on my UPDATE button, the underlying datarow state remains UNCHANGED. Is
this
| my fault, or it's just a buggy behaviour of databinding?
|
|
| 2) what's the correct way for binding to a combobox? Let's say I have a
form
| which displays some 'lookup' data in a combo: these data are bound to a
| parent table (e.g. Countries), and the selected item primary key is linked
| to a corresponding foreign key in a child table (e.g. Customer): the
| database looks like:
|
| TCustomer: customerID, countryID (the foreign key), ...
| TCountry: countryID (the primary key), country
|
| The countries combo will be bound to my (typed) dataset's table TCountry
as
| follows:
|
| myCombo.DataBindings.Add(new Binding("SelectedValue", myDataSet,
| "TCountry.countryID"));
| myCombo.DataSource = myDataSet.TCountry;
| myCombo.DisplayMember = "country";
| myCombo.ValueMember = "countryID";
|
| Now, when loading customer's data into the form, I'll have to "manually"
| select the correct country in the lookup combo:
|
| MyDataSet.TCountryRow row =
| myDataSet.TCountry.FindBycountryID(row.countryID);
| myCombo.SelectedIndex = myCombo.FindStringExact(row.country);
|
| And when updating the database, before calling EndCurrentEdit() and
updating
| via the data adapter I'll have to do the inverse:
|
| theCustomerRow.countryID = (int)_myCombo.SelectedValue;
|
| Is this OK, or is there a better way for doing it?
|
|
|
 
D

Dan

Thanks Kevin! I knew I had to call EndCurrentEdit, but exactly how it is
supposed to be called? That is, I called it from BindingContext, like:

BindingContext[myDataSet, "tableName"].EndCurrentEdit();

And this does not seem to work always. Shall I add also a call to the
EndCurrentEdit method of my dataset object, like:

myDayaSet.EndCurrentEdit()?

or what else? Thanks again...
 
K

Kevin Yu [MSFT]

Hi Dan,

Based on my experience, EndCurrentEdit() should work. However, you can also
try to call the EndEdit() method of the DataRow you're editing. Try to get
the reference of the row and call EndEdit() like the following:

myDataSet.Tables["tableName"].Rows[BindingContext[myDataSet,
"tableName"].Position].EndEdit();

But I think it should be the same as your method.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Dan" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: newbie: 2 databindings questions
| Date: Mon, 17 Nov 2003 18:23:03 +0100
| Lines: 13
| 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]>
| Newsgroups: microsoft.public.dotnet.framework.adonet
| NNTP-Posting-Host: ppp-217-133-155-123.cust-adsl.tiscali.it
217.133.155.123
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:66536
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Thanks Kevin! I knew I had to call EndCurrentEdit, but exactly how it is
| supposed to be called? That is, I called it from BindingContext, like:
|
| BindingContext[myDataSet, "tableName"].EndCurrentEdit();
|
| And this does not seem to work always. Shall I add also a call to the
| EndCurrentEdit method of my dataset object, like:
|
| myDayaSet.EndCurrentEdit()?
|
| or what else? Thanks again...
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top