all groups > dotnet windows forms databinding > september 2005 >
You're in the

dotnet windows forms databinding

group:

Databinding on a custom property not working properly



Databinding on a custom property not working properly anony NO[at]SPAM nospam.com
9/29/2005 2:01:05 PM
dotnet windows forms databinding: I have a form with a custom property called 'KamerID'. This property is
bound to a column (KAMERID) in a datatable (ABONNEES) in a dataset (dsAbo).

The property is bound at runtime:

this.DataBindings.Add("KamerID", dsAbo, "ABONNEES.KAMERID");


The code for the property:

[Bindable(true)]
public int KamerID
{
get
{
return _kamerID;
}

set
{
if (_kamerID == 0 && _aboID != 0)
{
_savedKamer = value;
}

_kamerID = value;

OracleCommand kamer = new OracleCommand(
"SELECT INSTELLINGEN.NAAM || ' | ' ||
KAMERS.KAMERNR " +
"FROM INSTELLINGEN, KAMERS " +
"WHERE KAMERS.INSTELLING = INSTELLINGEN.INSTELLING
AND " +
"KAMERID = " + _kamerID, _oradb);

_oradb.Open();
linkKamer.Text = kamer.ExecuteScalar().ToString();
_oradb.Close();

kamer.Dispose();
}
}

When the dataset is filled, the KamerID property is triggered and the text
of the LinkLabel changes. This works fine.
However, if the user changes the KamerID property, the change is not
reflected in the dataset.

In my Save method for the form, I have this code to make sure all current
edits are being saved to the dataset:

this.GetNextControl(this, true).Focus();
BindingContext[dsAbo.ABONNEES].EndCurrentEdit();

This works fine to correctly update all other databindings (e.g. the Text
property of textboxes), but not for the KamerID property.
How can I fix this for the KamerID property?

Re: Databinding on a custom property not working properly Bart Mermuys
9/29/2005 4:36:17 PM
Hi,

[quoted text, click to view]

You have to use the exact same DataSource and DataMember (excluding
fieldname) that you used for the binding, so in your case that would be :

BindingContext[ dsAbo, "ABONNEES" ].EndCurrentEdit();

That's not the same as what you're doing.

HTH,
greetings



[quoted text, click to view]

Re: Databinding on a custom property not working properly anony NO[at]SPAM nospam.com
9/30/2005 10:36:57 AM
Thanks for your reply!
You're absolutely right, the databinding is triggered now when I try to
save. However, when
BindingContext[dsAbo, "ABONNEES"].EndCurrentEdit();

is invoked in the Save method, the KamerID property of the form is also
triggered and the dataset pushes the old saved value to the property again.
Do I have to add some code to the KamerID to push te new value to the
dataset?

Thank you in advance,

Ruben

"Bart Mermuys" <bmermuys.nospam@hotmail.com> schreef in bericht
news:O$T2oMQxFHA.3812@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Re: Databinding on a custom property not working properly anony NO[at]SPAM nospam.com
9/30/2005 3:07:12 PM
I tried another approach by subclassing the linklabel and add the property
'KamerID' to this new class.
After adding an event handler KamerIDChanged to this property, everything
seems to work fine.

<anony@nospam.com> schreef in bericht
news:uy0EkoZxFHA.3712@TK2MSFTNGP10.phx.gbl...
[quoted text, click to view]

Re: Databinding on a custom property not working properly Bart Mermuys
9/30/2005 11:08:45 PM
Hi,

[quoted text, click to view]

No idea why it works better when the property is on a seperate control. A
basic setup seems to work for me when binding a property on a Form.

Two things can cause the value from the property to be written to the
DataSource, that's BindingContext[...].EndCurrentEdit or when the control
that has the property fires a validating event.

It's not required to have a KamerIDChanged event, but if you do have one you
must fire it when the property is changed, otherwise values will never be
persisted.

Greetings


[quoted text, click to view]

AddThis Social Bookmark Button