SoFunction
Updated on 2025-03-06

WinForm's BindingSource Basic Operation Example Tutorial

Usually when we are using data binding, the commonly used data sources include DataSet, DataTable, BindingList<T>, and strongly typed data sources. Today we will learn about the establishment of BindingSource through examples and share it with you for your reference.

Two uses of BindingSource:

(1) First, it provides an indirect layer that binds controls on the form to data. This is done by binding the BindingSource component to the data source and then binding the controls on the form to the BindingSource component. All further interactions with the data, including navigation, sorting, filtering, and updating, are done by calling the BindingSource component.
(2) Secondly, the BindingSource component can act as a strongly typed data source. Using the Add method to add a type to the BindingSource component creates a list of that type.

1. Basic operations on BindingSource - Add, delete, modify and check

The example code is as follows:

  public partial class Form1 : Form
  {
    //Note that the current DGV has been bound to the ID and Name columns    private BindingSource source = new BindingSource();
    public Form1()
    {
      InitializeComponent();
    }
    //Form loading    private void Form1_Load(object sender, EventArgs e)
    {
       = typeof(Custom);
      this. = ;
    }
    //Add to    private void button1_Click(object sender, EventArgs e)
    {
      (new Custom(1,"A"));
      (new Custom(2,"B"));
    }
    //delete    private void button2_Click(object sender, EventArgs e)
    {
      (0);
    }
    //Sort [Ask a Problem]    private void button3_Click(object sender, EventArgs e)
    {
       = "ID ASC";
      (false);
    }
    //Filter [Ask for question]    private void button4_Click(object sender, EventArgs e)
    {
       = "ID = 1";
      (false);
    }
    //Move down    private void button5_Click(object sender, EventArgs e)
    {
      ();
      (());
    }
    //Move up    private void button9_Click(object sender, EventArgs e)
    {
      ();
      (());
    }
    //Get the current item    private void button6_Click(object sender, EventArgs e)
    {
      Custom custom = (Custom);
      (" Where : " + (custom).ToString());
      (" : " + );
    }
    //Modify the current item    private void button7_Click(object sender, EventArgs e)
    {
      Custom custom = (Custom);
       = "Modified Value";
      ();
    }
    //Delete the current item    private void button8_Click(object sender, EventArgs e)
    {
      Custom custom = (Custom);
      (custom);
    }
  }
  //Custom class fields must be publicly available  public class Custom
  {
    public Custom()
    { }
    public Custom(int ID, string Name)
    {
       = ID;
       = Name;
    }
    private int id;
    public int ID
    {
      get { return id; }
      set { id = value; }
    }
    private string name;
    public string Name
    {
      get { return name; }
      set { name = value; }
    }
  }

2. The following example shows how to bind DBNull values ​​in two different situations.

The first case shows how to set the NullValue of the string attribute; the second case shows how to set the NullValue of the image attribute.

The following example shows how to bind a DBNull value in two different cases. The first case shows how to set the NullValue of the string attribute; the second case shows how to set the NullValue of the image attribute.

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace DBNullCS
{
  public class Form1 : Form
  {
    public Form1()
    {    
       += new EventHandler(Form1_Load);
    }
    // The controls and components we need for the form.
    private Button button1;
    private PictureBox pictureBox1;
    private BindingSource bindingSource1;
    private TextBox textBox1;
    private TextBox textBox2;
    // Data table to hold the database data.
    DataTable employeeTable = new DataTable();
    void Form1_Load(object sender, EventArgs e)
    {
      // Basic form setup.
      this.pictureBox1 = new PictureBox();
      this.bindingSource1 = new BindingSource();
      this.textBox1 = new TextBox();
      this.textBox2 = new TextBox();
      this.button1 = new Button();
      this. = new (20, 20);
      this. = new (174, 179);
      this. = new (25, 215);
      this. = true;
      this. = new (25, 241);
      this. = true;
      this. = new (200, 103);
      this. = "Move Next";
      this. += new (this.button1_Click);
       = new (292, 273);
      (this.button1);
      (this.textBox2);
      (this.textBox1);
      (this.pictureBox1);
      (false);
      ();
      // Create the connection string and populate the data table
      // with data.
      string connectionString = "Integrated Security=SSPI;" +
        "Persist Security Info = False;Initial Catalog=Northwind;" +
        "Data Source = localhost";
      SqlConnection connection = new SqlConnection();
       = connectionString;
      SqlDataAdapter employeeAdapter = 
        new SqlDataAdapter(new SqlCommand("Select * from Employees", connection));
      ();
      (employeeTable);
      // Set the DataSource property of the BindingSource to the employee table.
       = employeeTable;
      // Set up the binding to the ReportsTo column.
      Binding reportsToBinding = ("Text", bindingSource1, 
        "ReportsTo", true);
      // Set the NullValue property for this binding.
       = "No Manager";
      // Set up the binding for the PictureBox using the Add method, setting
      // the null value in method call.
      ("Image", bindingSource1, "Photo", true, 
        , new Bitmap(typeof(Button), ""));
      // Set up the remaining binding.
      ("Text", bindingSource1, "LastName", true);
    }
    // Move through the data when the button is clicked.
    private void button1_Click(object sender, EventArgs e)
    {
      ();
    }
    [STAThread]
    static void Main()
    {
      ();
      (new Form1());
    }
  }
}

I hope this example will be helpful to everyone’s C# programming learning!