User Control binding

E

Erick Gonzales

Hi, i´ve created a user control that contains a TextBox and a Label, lets
call this control CText.
CText has properties like Value and Text, where Text is used to display
formatted Value.
Well, i´m trying to create a new databind using:
cText1.DataBindings.Add("Value", wDataTable ,"TableField1" );

_CM = (CurrencyManager) this.BindingContext[wDataTable ];

_CM.AddNew() ;

Then, when submitting the form (winform), i call

_CM.EndCurrentEdit();

....where it is supposed that the bindingcontext datasource should be
updated..., but this is not working as expected.

-> Tried the same with a TextBox and it works....

-> MSDN : You can simple-bind to any object that derives from the
System.Windows.Forms.Control class...., and UserControl does, so what´s the
problem??

any suggestion?

ErickG
 
T

Tom Krueger [MSFT]

It sounds like you are trying to bind a datatable to a control, however,
when the data on the control is changed, those changes are not reflected in
the datatable.

If this is the case you may be running into the problem where the control
needs to send back notifications that the property value has changed. To do
so, add event handlers to your control and raise them when the properties
are set.

private event ValueChanged as EventHandler;
private event TextChanged as EventHandler

public string Text {
get { return _text; }
set {
_text = value;
TextChanged(this, new EventArgs())
}
}

*** NOTE: Code may not be perfect, I typed it without the VisualStudio and
from memory. It should be close though.

Keep in mind the event declarations need to be ProertyNameChanged, so that
the binding listens for it.


--
Tom Krueger

My Blog - http://weblogs.asp.net/tom_krueger
Smart Client DevCenter - http://msdn.microsoft.com/smartclient/
Mobile DevCenter - http://msdn.microsoft.com/mobility

This posting is provided "as is" with no warranties and confers no rights.
 
E

Erick Gonzales

Thank you Tom

I have my bindable controls working now!

ErickG



Tom Krueger said:
It sounds like you are trying to bind a datatable to a control, however,
when the data on the control is changed, those changes are not reflected
in the datatable.

If this is the case you may be running into the problem where the control
needs to send back notifications that the property value has changed. To
do so, add event handlers to your control and raise them when the
properties are set.

private event ValueChanged as EventHandler;
private event TextChanged as EventHandler

public string Text {
get { return _text; }
set {
_text = value;
TextChanged(this, new EventArgs())
}
}

*** NOTE: Code may not be perfect, I typed it without the VisualStudio and
from memory. It should be close though.

Keep in mind the event declarations need to be ProertyNameChanged, so that
the binding listens for it.


--
Tom Krueger

My Blog - http://weblogs.asp.net/tom_krueger
Smart Client DevCenter - http://msdn.microsoft.com/smartclient/
Mobile DevCenter - http://msdn.microsoft.com/mobility

This posting is provided "as is" with no warranties and confers no rights.

Erick Gonzales said:
Hi, i´ve created a user control that contains a TextBox and a Label, lets
call this control CText.
CText has properties like Value and Text, where Text is used to display
formatted Value.
Well, i´m trying to create a new databind using:
cText1.DataBindings.Add("Value", wDataTable ,"TableField1" );

_CM = (CurrencyManager) this.BindingContext[wDataTable ];

_CM.AddNew() ;

Then, when submitting the form (winform), i call

_CM.EndCurrentEdit();

...where it is supposed that the bindingcontext datasource should be
updated..., but this is not working as expected.

-> Tried the same with a TextBox and it works....

-> MSDN : You can simple-bind to any object that derives from the
System.Windows.Forms.Control class...., and UserControl does, so what´s
the problem??

any suggestion?

ErickG
 

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