WinForm BindingSource Basic Operation example tutorial

  • 2020-09-16 07:45:20
  • OfStack

In general, when we do data binding, the commonly used data sources are DataSet, DataTable, BindingList < T > , and strongly typed data sources. Today, we will learn about BindingSource 1 through examples, and share it with you for your reference.

Two USES of BindingSource:

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

1. Basic operation of BindingSource -- add, delete, change and check

The example code is as follows:


  public partial class Form1 : Form
  {
    // Note the current DGV Bound to  ID  and  Name  column 
    private BindingSource source = new BindingSource();
    public Form1()
    {
      InitializeComponent();
    }
    // Form loads 
    private void Form1_Load(object sender, EventArgs e)
    {
      this.source.DataSource = typeof(Custom);
      this.dataGridView1.DataSource = this.source;
    }
    // add 
    private void button1_Click(object sender, EventArgs e)
    {
      this.source.Add(new Custom(1,"A"));
      this.source.Add(new Custom(2,"B"));
    }
    // delete 
    private void button2_Click(object sender, EventArgs e)
    {
      this.source.RemoveAt(0);
    }
    // The sorting   [Question] 
    private void button3_Click(object sender, EventArgs e)
    {
      this.source.Sort = "ID ASC";
      this.source.ResetBindings(false);
    }
    // screening   [Question] 
    private void button4_Click(object sender, EventArgs e)
    {
      this.source.Filter = "ID = 1";
      this.source.ResetBindings(false);
    }
    // Move down the 
    private void button5_Click(object sender, EventArgs e)
    {
      this.source.MoveNext();
      MessageBox.Show(this.source.Position.ToString());
    }
    // Move up 
    private void button9_Click(object sender, EventArgs e)
    {
      this.source.MovePrevious();
      MessageBox.Show(this.source.Position.ToString());
    }
    // Get the current item 
    private void button6_Click(object sender, EventArgs e)
    {
      Custom custom = (Custom)this.source.Current;
      MessageBox.Show("  The position of the  : " + this.source.IndexOf(custom).ToString());
      MessageBox.Show("custom.Name : " + custom.Name);
    }
    // Modify current item 
    private void button7_Click(object sender, EventArgs e)
    {
      Custom custom = (Custom)this.source.Current;
      custom.Name = " The modified value ";
      this.source.ResetCurrentItem();
    }
    // Delete current item 
    private void button8_Click(object sender, EventArgs e)
    {
      Custom custom = (Custom)this.source.Current;
      this.source.Remove(custom);
    }
  }
  // Custom class   Field properties must be made public 
  public class Custom
  {
    public Custom()
    { }
    public Custom(int ID, string Name)
    {
      this.ID = ID;
      this.Name = 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 the DBNull value in two different cases.

Case 1 shows how to set NullValue of the string property; The second case shows how to set the NullValue of the image properties.

The following example shows how to bind the DBNull value in two different cases. Case 1 shows how to set NullValue for string properties; Case 2 shows how to set NullValue of the image properties.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace DBNullCS
{
  public class Form1 : Form
  {
    public Form1()
    {    
      this.Load += 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.pictureBox1.Location = new System.Drawing.Point(20, 20);
      this.pictureBox1.Size = new System.Drawing.Size(174, 179);
      this.textBox1.Location = new System.Drawing.Point(25, 215);
      this.textBox1.ReadOnly = true;
      this.textBox2.Location = new System.Drawing.Point(25, 241);
      this.textBox2.ReadOnly = true;
      this.button1.Location = new System.Drawing.Point(200, 103);
      this.button1.Text = "Move Next";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.Add(this.button1);
      this.Controls.Add(this.textBox2);
      this.Controls.Add(this.textBox1);
      this.Controls.Add(this.pictureBox1);
      this.ResumeLayout(false);
      this.PerformLayout();
      // 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();
      connection.ConnectionString = connectionString;
      SqlDataAdapter employeeAdapter = 
        new SqlDataAdapter(new SqlCommand("Select * from Employees", connection));
      connection.Open();
      employeeAdapter.Fill(employeeTable);
      // Set the DataSource property of the BindingSource to the employee table.
      bindingSource1.DataSource = employeeTable;
      // Set up the binding to the ReportsTo column.
      Binding reportsToBinding = textBox2.DataBindings.Add("Text", bindingSource1, 
        "ReportsTo", true);
      // Set the NullValue property for this binding.
      reportsToBinding.NullValue = "No Manager";
      // Set up the binding for the PictureBox using the Add method, setting
      // the null value in method call.
      pictureBox1.DataBindings.Add("Image", bindingSource1, "Photo", true, 
        DataSourceUpdateMode.Never, new Bitmap(typeof(Button), "Button.bmp"));
      // Set up the remaining binding.
      textBox1.DataBindings.Add("Text", bindingSource1, "LastName", true);
    }
    // Move through the data when the button is clicked.
    private void button1_Click(object sender, EventArgs e)
    {
      bindingSource1.MoveNext();
    }
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
  }
}

I hope this example will be helpful for you to learn C# programming!


Related articles: