Export (0) Print
Expand All

How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource

When you use data-bound controls, you sometimes have to respond to changes in the data source when the data source does not raise list-changed events. When you use the BindingSource component to bind your data source to a Windows Forms control, you can notify the control that your data source has changed by calling the ResetBindings method.


The following code example demonstrates using the ResetBindings method to notify a bound control about an update in the data source.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace System_Windows_Forms_UpdateBinding
	class Form1 : Form
        // Declare the objects on the form.
        private Label label1;
        private Label label2;
        private TextBox textBox1;
        private TextBox textBox2;
        private Button button1;
        private BindingSource bindingSource1;
        ArrayList states;

		public Form1()
            // Basic form setup.
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.button1.Location = new System.Drawing.Point(12, 18);
            this.button1.Size = new System.Drawing.Size(119, 38);
            this.button1.Text = "RemoveAt(0)";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.textBox1.Location = new System.Drawing.Point(55, 75);
            this.textBox1.ReadOnly = true;
            this.textBox1.Size = new System.Drawing.Size(119, 20);
            this.label1.Location = new System.Drawing.Point(12, 110);
            this.label1.Size = new System.Drawing.Size(43, 14);
            this.label1.Text = "Capital:";
            this.label2.Location = new System.Drawing.Point(12, 78);
            this.label2.Size = new System.Drawing.Size(34, 14);
            this.label2.Text = "State:";
            this.textBox2.Location = new System.Drawing.Point(55, 110);
            this.textBox2.Size = new System.Drawing.Size(119, 20);
            this.textBox2.ReadOnly = true;
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Text = "Form1";

            // Create an ArrayList containing some of the State objects.
            states = new ArrayList();
            states.Add(new State("California", "Sacramento"));
            states.Add(new State("Oregon", "Salem"));
            states.Add(new State("Washington", "Olympia"));
            states.Add(new State("Idaho", "Boise"));
            states.Add(new State("Utah", "Salt Lake City"));
            states.Add(new State("Hawaii", "Honolulu"));
            states.Add(new State("Colorado", "Denver"));
            states.Add(new State("Montana", "Helena"));

            bindingSource1 = new BindingSource();

            // Bind BindingSource1 to the list of states.
            bindingSource1.DataSource = states;

            // Bind the two text boxes to properties of State.
            textBox1.DataBindings.Add("Text", bindingSource1, "Name");
            textBox2.DataBindings.Add("Text", bindingSource1, "Capital");

        private void button1_Click(object sender, EventArgs e)
            // If items remain in the list, remove the first item. 
            if (states.Count > 0)

                // Call ResetBindings to update the textboxes.

        static void Main()
            Application.Run(new Form1());

        // The State class to add to the ArrayList.
		private class State
			private string stateName;
			public string Name 
				get {return stateName;}

			private string stateCapital;
			public string Capital 
				get {return stateCapital;}

			public State ( string name, string capital)
                stateName = name;
                stateCapital = capital;

import System.*;
import System.Collections.Generic.*;
import System.ComponentModel.*;
import System.Data.*;
import System.Data.Common.*;
import System.Diagnostics.*;
import System.Drawing.*;
import System.Data.SqlClient.*;
import System.Windows.Forms.*;

// This demonstrates using a BindingSource component to update
// the schema of a result set from a database query.
public class Form1 extends System.Windows.Forms.Form
    // Clicking this button causes a new column to be added to the
    // result set.
    private Button addNewColumnBtn = new Button();

    // This DataGridView control displays the result set data.
    private DataGridView customersDataGridView = new DataGridView();

    // This BindingSource component binds the DataGridView to the
    // result set.
    private BindingSource customersBindingSource = new BindingSource();

    // This counter will be used to name new columns uniquely.
    private int newColumnCount = 0;

    public Form1()
        // Set up the form.
        this.set_Size(new Size(800, 800));
        this.add_Load(new EventHandler(Form1_Load));
        // Set up the "Add New Column" button.
        this.addNewColumnBtn.set_Text("Add New Column");
        this.addNewColumnBtn.add_Click(new EventHandler(addNewColumnBtn_Click));
        // Set up the DataGridView control.
    } //Form1

    private void Form1_Load(Object sender, EventArgs e)
        // Open a connection to the database.
        String connectString = "Integrated Security=SSPI;"
            + "Persist Security Info=False;" 
            + "Initial Catalog=Northwind;Data Source=localhost";
        SqlConnection connection = new SqlConnection();
        // Execute the query.
        SqlDataAdapter adapter = new SqlDataAdapter(
            "Select * From Customers", connection);
        DataTable table = new DataTable();
        // Attach the data source to the BindingSource control.
        // Handle the ListChanged event.
            new ListChangedEventHandler(customersBindingSource_ListChanged));
        // Attach the BindingSource to the DataGridView.
    } //Form1_Load

    // This event handler adds a new column to the result set
    // and calls the ResetBindings method to tell the BindingSource 
    // component that the schema of the result set has changed.
    private void addNewColumnBtn_Click(Object sender, EventArgs e)
        // Increment the new column counter.
        // Add a new column to the result set.
        DataTable table = (DataTable)this.customersBindingSource.
        table.get_Columns().Add("NewColumn" + System.Convert.ToString(
            this.newColumnCount), DataColumn.class.ToType());
        // Tell the BindingSource component that the schema of 
        // the result set has changed.
    } //addNewColumnBtn_Click

    // This event handler is used to report changes to the 
    // BindingSource component's underlying list.
    private void customersBindingSource_ListChanged(Object sender, ListChangedEventArgs e)
    } //customersBindingSource_ListChanged

    /** @attribute STAThread()
    public static void main(String[] args)
        Application.Run(new Form1());
    } //main
} //Form1

Compiling the Code

This example requires:

  • References to the System, System.Drawing and System.Windows.Forms assemblies.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line (Visual Basic) or Command-Line Building. You can also build this example in Visual Studio by pasting the code into a new project. How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio

See Also

Community Additions

© 2015 Microsoft