Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataBinding to a WinForm

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

In the automatically generated form code, each of the text boxes has a line of code to bind the datasource to the text box

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

Code automatically generated by the C# ide for each text box:

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 isn't set until after the DataBinding) in the IDE generated code.

When I run the project, the form is displayed with the default values in the TextBoxes. However when the SaveButton is pressed to serialize the properties in the MyForm.CustomerInfo subclass, they are all null. Since these values will only be changed from the form I was hoping that I didn't have to implement the interface INotifyPropertyChanged.

Am I missing something basic or simple?

The code for the form including the serialization of the data is attached 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 happen upon this question

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; }
        }
    }
}
like image 698
DarwinIcesurfer Avatar asked Jun 18 '11 04:06

DarwinIcesurfer


1 Answers

First of all you need to decide whether to use databinding or manipulate Text property directly. Those two approaches should not be mixed together.

If you want to use databinding than you are missing one line in the code:

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

You need to let your customerInfoBindingSource know about the data source.

If you add this line, then the Text that is assigned in design time will be overridden by the text from your bound data source. If you want to use binding you should manipulate with the data source instead of setting Text fields directly. Like this:

public Form1()
{
    InitializeComponent();

    ci.CustomerName = "TestCustomerName";
    customerInfoBindingSource.DataSource = ci;
}
like image 100
Alex Aza Avatar answered Sep 16 '22 13:09

Alex Aza