WinForm BindingSource Basic Operation Example Tutorial

Source: Internet
Author: User

Usually when we are doing data binding, the common data sources are datasets, DataTable, bindinglist<t>, and strongly typed data sources. Today we come to learn from the example of BindingSource formation, share to everyone for reference.


Two uses of BindingSource:

(1) First, it provides an indirect layer that binds controls on a 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 invoking the BindingSource component.

(2) Second, the BindingSource component can act as a strongly typed data source. Adding a type to the BindingSource component by using the Add method creates a list of that type.


First, the basic operation of the BindingSource--adding and deleting changes

The instance code is as follows:

  Public partial class Form1:form {//Note the current DGV is already bound to the 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);      }//Sort "problematic" private void button3_click (object sender, EventArgs e) {this.source.Sort = "ID ASC";    This.source.ResetBindings (FALSE);      }//filter "problematic" private void Button4_Click (object sender, EventArgs e) {this.source.Filter = "ID = 1";    This.source.ResetBindings (FALSE);  }//Move Down 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 ()); }//Gets the current item private void Button6_click (object sender, EventArgs e) {Custom custom = (custom) This.source.Curre      nt MessageBox.Show ("in place:" + this.source.IndexOf (custom).      ToString ()); MessageBox.Show ("Custom. Name: "+ Custom.    Name); }//Modify the current item private void Button7_click (object sender, EventArgs e) {Custom custom = (custom) This.source.Curre      nt Custom.      Name = "Modified value";    This.source.ResetCurrentItem (); }//Delete the current item private void Button8_click (object sender, EventArgs e) {Custom custom = (custom) This.source.Curre      nt    This.source.Remove (custom); }}//The custom class field must have a property open public class Custom {public custom () {} public custom (int ID, string Name) {T His.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;} }  }


Second, the following example shows how to bind the DBNull value in two different situations.

The first scenario shows how to set the Nullvalue of a string property, and the second case shows how to set the Nullvalue of the image properties.

The following example shows how to bind a DBNull value in two different situations. The first scenario shows how to set the Nullvalue of a string property, and the second case shows how to set the 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 of we need for the form.    Private Button button1;    Private PictureBox PictureBox1;    Private BindingSource BindingSource1;    Private TextBox TextBox1;    Private TextBox TextBox2;    Data table to 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 of the C # program design for everyone to help!

In addition to the Declaration,Running GuestArticles are original, reproduced please link to the form of the address of this article
WinForm BindingSource Basic Operation Example Tutorial

This address: http://www.paobuke.com/develop/c-develop/pbk23599.html






Related content C # read XML multilevel child nodes C # workaround for passing multiple parameters to a thread (two) differences and relationships between foreground and background threads in C # Brush pen method for drawing smooth-mode curves
C # yield error in WCF use (ii) C # interpolation algorithm example Analysis C # method for calculating age accurately analysis C + + and-= in-depth research

WinForm BindingSource Basic Operation Example Tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.