Linking Textboxes to a dataset using a BindingSource

10,074

Solution 1

Try using the DataBinding collection of the textboxes.

Something like this:

uxDescriptionTextBox.DataBindings.Add("Text", 
                                      definitionsBindingSource,
                                      fieldInTable);

Solution 2

Have added the full source code (highlighting exactly your requirement) here - http://sdrv.ms/NyXHdu. Download > Open the solution in VS2010 > Hit F5

[Update]

  1. Double click on Form.cs designer and observe the productListBindingSource. It bound to a custom object - The ProductList class

  2. Then see the properties of the TextBoxes & ComboBox and observe the DataBindings > Text property. They are bound to the productListBindingSource's individual item. See Image below.

enter image description here

Courtsey - http://www.apress.com/9781590594391/ [Chapter 8]

Share:
10,074
whytheq
Author by

whytheq

Current addictions: DAX / POWERSHELL Time served with: (T-)sql / MDX / VBA / SSRS Would like more time for the following: C# Python Maxim: if you build something idiot-proof, the world will build a better idiot

Updated on June 05, 2022

Comments

  • whytheq
    whytheq almost 2 years

    This is the section of the form I am working on:

    enter image description here

    The following code links the BindingNavigator to the dataset using a bindingSource. Can I use this binding source to hook up the two text boxes to the data?

    Do I simply need to use a property of the textboxes or is this more involved?

    i.e when the form loads the first record's fields "Work Phrase" and "Description" will be displayed and when I scroll using the navigator the values in these boxes will change accordingly.

    public partial class uxRevisionHelperForm : Form
    {
    
        public SqlCeConnection conn = new SqlCeConnection(ConfigurationManager.ConnectionStrings["WindFormAppRevisionHelper.Properties.Settings.DefinitionsDBConnectionString"].ConnectionString);
        BindingSource definitionsBindingSource = new BindingSource();
    
        public uxRevisionHelperForm()
        {
            InitializeComponent();
            uxDescriptionTextBox.AutoSize = true;
            this.hookUpBindingNavigator();
        }
    
        public void hookUpBindingNavigator()
        {            
    
            SqlCeDataAdapter da = new SqlCeDataAdapter(new SqlCeCommand("Select * From tb_Definitions",conn));
            DataSet ds = new DataSet("Helper");
            ds.Tables.Add("DefinitionsTable");
            da.Fill(ds.Tables["DefinitionsTable"]);
    
            // Assign the BindingSource.
            this.uxBindingNavigator.BindingSource = this.definitionsBindingSource;
            this.definitionsBindingSource.DataSource = ds.Tables["DefinitionsTable"];
    
        }
    
  • whytheq
    whytheq almost 12 years
    +1 for the little app for me to nose around - I assume the answer to my question is in the file form1.designer.cs ...or is that all the boiler plate code?
  • whytheq
    whytheq almost 12 years
    seems to work; I found this on MSDN which has a similar piece of code but with an extra argument in the Add method