DataBinding for WinForm

I have a form (CustomerInfoForm) with 10 text fields. The default Text property for each of the TextBoxes is determined at design time. The CustomerInfoForm.CustomerInfo subclass contains properties for storing data entered in the form. A subclass containing the data will be serialized in XML.

In code with an automatically generated form, each text field has a line of code to bind the data source to the text field

 this.customerInfoBindingSource = new System.Windows.Forms.BindingSource(this.components); 

Code automatically generated by C # ide for each text field:

 this.txtCustomer.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.customerInfoForm_CustomerInfoBindingSource, "CustomerName", true)); this.txtCustomer.Location = new System.Drawing.Point(60, 23); this.txtCustomer.Name = "txtCustomer"; this.txtCustomer.Size = new System.Drawing.Size(257, 20); this.txtCustomer.TabIndex = 0; this.txtCustomer.Text = "CustomerName"; 

(I noticed that the Text property is not set until after the DataBinding) in the generated IDE code.

When I run the project, the form is displayed with the default values ​​in TextBoxes . However, when SaveButton clicked to serialize properties in the MyForm.CustomerInfo subclass, they are all zero. Since these values ​​will only be changed from the form, I was hoping that I did not need to implement the INotifyPropertyChanged interface.

Am I missing something basic or simple?

The form code, including data serialization, is given below.

 using System; using System.Windows.Forms; using System.Xml.Serialization; using System.IO; using System.Runtime.Serialization; namespace SimpleCustomerInfo { // You must apply a DataContractAttribute or SerializableAttribute // to a class to have it serialized by the DataContractSerializer. public partial class CustomerInfoForm : Form { CustomerInfo ci = new CustomerInfo(); public CustomerInfoForm() { InitializeComponent(); } private void btnSave_Click(object sender, EventArgs e) { DataContractSerializer serializer = new DataContractSerializer(typeof(CustomerInfo)); FileStream writer = new FileStream(@"C:\Users\Me\temp\testme.xml", FileMode.Create); serializer.WriteObject(writer,ci); writer.Close(); } [DataContract(Name = "Customer", Namespace = "net.ElectronicCanvas")] public class CustomerInfo { [DataMember] public string CustomerName { get; set; } [DataMember] public PhoneInfo PhonePrimary { get; set; } [DataMember] public PhoneInfo PhoneDays { get; set; } [DataMember] public PhoneInfo PhoneEvening { get; set; } } public class PhoneInfo { public string number { get; set; } public string type { get; set; } public bool textOk { get; set; } } } } 

EDIT - for others that may arise on this subject

 using System; using System.Windows.Forms; using System.Xml.Serialization; using System.IO; using System.Runtime.Serialization; namespace SimpleCustomerInfo { public partial class CustomerInfoForm : Form { CustomerInfo ci; public CustomerInfoForm() { InitializeComponent(); ci = new CustomerInfo(); ci.CustomerName = "My Customer Name"; ci.PhoneDays.number = "888-888-8888"; customerInfoForm_CustomerInfoBindingSource.DataSource = ci; } private void btnSave_Click(object sender, EventArgs e) { DataContractSerializer serializer = new DataContractSerializer(typeof(CustomerInfo)); FileStream writer = new FileStream(@"C:\Users\me\temp\testme.xml", FileMode.Create); serializer.WriteObject(writer,ci); writer.Close(); } // You must apply a DataContractAttribute or SerializableAttribute // to a class to have it serialized by the DataContractSerializer. [DataContract(Name = "Customer", Namespace = "net.ElectronicCanvas")] public class CustomerInfo { [DataMember] public string CustomerName { get; set; } [DataMember] public PhoneInfo PhonePrimary { get; set; } [DataMember] public PhoneInfo PhoneDays { get; set; } [DataMember] public PhoneInfo PhoneEvening { get; set; } // Constructor is needed to instantiate the PhoneInfo classes // within the CustomerInfo class public CustomerInfo() { PhonePrimary = new PhoneInfo(); PhoneDays = new PhoneInfo(); PhoneEvening = new PhoneInfo(); } } public class PhoneInfo { public string number { get; set; } public string type { get; set; } public bool textOk { get; set; } } } } 
+6
source share
1 answer

First of all, you need to decide whether to directly use data binding or manage the Text property. These two approaches should not be mixed.

If you want to use data binding than one line is missing in the code:

 public Form1() { InitializeComponent(); customerInfoBindingSource.DataSource = ci; // This is the missing line } 

You must tell your customerInfoBindingSource about the data source.

If you add this line, the Text assigned at design time will be overridden by the text from your linked data source. If you want to use binding, you should manipulate the data source, and not directly set the Text fields. Like this:

 public Form1() { InitializeComponent(); ci.CustomerName = "TestCustomerName"; customerInfoBindingSource.DataSource = ci; } 
+3
source

Source: https://habr.com/ru/post/890805/


All Articles