简体   繁体   中英

C# Windows Forms Data Binding

I'm using Visual c# in Windows forms and I'm very stuck.

myTxtBx.DataBindings.Add(new Binding("Text", myBindingSource, "Name", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty));

My issue is that myTxtBx receives no updates at all; I actually changed it to a text box because it wouldn't work with a label either.

Within the code I am assigning myTxtBx a value, as this is how it has to be done in this situation.

I need a way that the bindings will take the value from the text box, despite not being updated at all.

I have tried changing it to DataSourceUpdateMode.OnValidation and forcing it to validate before doing the DB stuff, but the DataBindings still didn't take the value. I also tried using WriteValue() and that didn't seem to work either..

Any suggestions would be appreciated!

checked your issue on sample. I have Form with two TextBox , bindings looks like:

this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource1, "Text", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource1, "Text", true));

Binding source this.bindingSource1 is type of System.Windows.Forms.BindingSource

Here is the code of setting binding source data:

_bindedObject = new DataObject{ Text = "Initialized value"};
bindingSource1.DataSource = _bindedObject;

An the _bindedObject is simple class with property Text:

[Serializable]
public class DataObject
{
    public string Text { get; set; } 
}

textBox1 keeps updating data on every change of containing text and textBox2 is refreshing. textBox2 is updating data when it lose focus (causes validation) or when the bindingSource1.EndEdit(); is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM