Using DataSource with TextBox

I usually program in C ++, so all this DataSource / DataSet / Binding stuff confuses me. Hope you guys can help.

Basically I am writing an editor for an XML based file format (specifically OFX for financial data). I used xsd.exe in my schema to deserialize uploaded files into nice, simple old classes. I found a DataGridView that is brilliant and I can just set its DataSource property to one of the collections I'm interested in (in particular the list of transactions) and when I refer to the values ​​these changes will be reflected in the loaded deserialized file, which I can then serialize on save. But when I want to "display" just a simple string in the TextBox (like the account number), I cannot use this clever method in the TextBoxes, it doesn't seem to have a DataSource member ... Using their Text propertyjust sets the text once and doesn't reflect the changes back to the underlying object, so the save must grab the values ​​from the control first. I would like it to be automatic, as for the DataGridView.

I tried to tinker with DataBindings, but I have no idea what to use as propertyName or dataMember, so I'm not sure if this is what I should be using:

accountNumberTextBox.DataBindings.Add(new Binding("???", myDocument.accountNumber, "???");

      

Am I missing something really obvious? I hope so!

+2


source to share


2 answers


What you are missing is what is string

immutable in .NET. Thus, for a binding to make sense, the value string

must be encapsulated by something else. The data binding system then replaces the existing string with a new one when the user enters a value.

Something else that encapsulates string

could be DataTable

either a simple old class that includes change notification. The best way to provide this change notification is to implement an interface INotifyPropertyChanged

.

For example:



public class Document : INotifyPropertyChanged
{
    private string _accountNumber;

    public string AccountNumber
    {
        get { return _accountNumber; }
        set
        {
            if (_accountNumber != value)
            {
                _accountNumber = value;
                //this tells the data binding system that the value has changed, so the interface should be updated
                OnPropertyChanged("AccountNumber");
            }
        }
    }

    //raised whenever a property value on this object changes. The data binding system attaches to this event
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged:

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

      

So, your data binding will look like this:

var document = ...; //get document from somewhere
//bind the Text property on the TextBox to the AccountNumber property on the Document
textBox1.DataBindings.Add("Text", document, "AccountNumber");

      

+4


source


accountNumberTextBox.DataBindings.Add("Text",
                                      myDocumnt.Tables["your_table"],
                                      "table_field");

      

Example



DataSet ds = new DataSet("DB");
DataTable dt = new DataTable("myTable");
dt.Columns.Add("Name");
dt.Rows.Add("PP");
dt.Rows.Add("QQ");
ds.Tables.Add(dt);

textBox1.DataBindings.Add("Text", ds.Tables["myTable"], "Name");

      

+2


source







All Articles