DataBinding problem

  • Thread starter Thread starter Massimo Zaccarin
  • Start date Start date
M

Massimo Zaccarin

I have some problems with databindings in textboxes:
- I've bound the "Text" property to a specific field of a dataTable, and I'm
able to read the correct calue
- When I change the value from the UI, I'm able to save the DataRow (with
"AcceptChanges")
- When I change the "Text" property from code, the field isn't saved (it
saves all the fields that have been modified from the UI or that have
received the focus, but there is no code in the "Validating", "Validated",
etc. events)

Do I have to modify the datarow instead of the "Text" property?
Isn't it the same as typing into the textbox?

Thanks
Max
 
Hi Massimo,


You may call BindingContext[dataset,"tableName"].EndCurrentEdit() after
changing the TextBox.Text property, this method will force the controls
bound to this datasrouce leave the "Edit" mode and update the data to the
data source. But it will not fire the Validating/Validated event on the
TextBox, these two events are only fired in FocusChange on the control and
largely designed for UI input.
So you need do validation before setting the data.


In addition, I think it would be better to modify the underlying datasource
directly if you need modify the value by code, from your description, I
guess you are trying to modify some columns on the current row. You may try
using the CurrencyManager.Current property, it can be casted to a
DataRowView object for the current row. Here is a small sample for this way:
<code>
CurrencyManager cm =
(CurrencyManager)BindingContext[dataSet11,"Categories"];
DataRowView drv = cm.Current as DataRowView;

if (doValidation(newData) == true)
{
// force all bounded control leave the "edit" mode
cm.EndCurrentEdit();
drv["CategoryName"] = newData;
}
</code>


Does it resolve your problem?
Please feel free to reply this thread if you have anything unclear on it.
Thanks!



Best regards,

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

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
I've found, your suggestions partially helped me...
I didn't know that I have to call the datarow's "BeginEdit" method before
changing bound control's values from code (it doesn't seem to be necessary
if changes are made from the UI).
Now it works fine.

Thanks
 
Back
Top