DataBindings Usage Example Analysis _c# tutorial in C #

Source: Internet
Author: User

The examples in this article describe DataBindings usage in C #. Share to everyone for your reference, specific as follows:

In C # operation of the database process, for the general text control, such as Textbox,label, and so on, we assign value directly using a similar textbox.text=**** way to do, this way is indeed the easiest way in a sense, but for the more complex space, For example, DataGridView, this time, we generally use the binding data source datagridview1.datasource=****, if the data source slightly changed, then only need to recall the binding again. It can be said that this way is one-way, from the database to the UI, but there is no way to implement the data source changes, do not have to rebind DataGridView Let it automatically refresh the data, of course, here to mention is databinding.

The code is as follows

Form2.cs Code:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms; 
    namespace Databindingstest {public partial class Form2:form {public Form2 () {InitializeComponent (); } mydatasource myDataSource = new myDataSource ();
    Apply to the second way public int Num {get; set;///apply to the Third Way public list<blognew> blognews {get; set;}///apply to the fourth method The public bindinglist<blognew> Blognewsregardui {get; set;}//Applies to the DataGridView UI update private void Mainfrm_load ( Object sender, EventArgs e) {#region Test one/************************************************ * First value:
       Where to bind to the textbox * Second value: What is the data source * Third value: What attributes should be taken from the data source * Fourth value: whether to open Data format * Fifth value: When to enable data source binding * *********************************************/textBox1.DataBindings.Add ("Text", TrackBar1, "Value", False, Dataso Urceupdatemode.onpropertychanged); #endregion #region Test Two/********************************************* * This is mainly through an external class, as a data source * * /myDataSource.
      MyValue = "This is a test";
      TEXTBOX2.DATABINDINGS.ADD ("Text", myDataSource, "MyValue", false, datasourceupdatemode.onpropertychanged); #endregion #region Test Three/***************************************** * This is mainly through the property itself, as a data source *******
      /Num = 5;
      TEXTBOX3.DATABINDINGS.ADD ("Text", this, "Num", false, datasourceupdatemode.onpropertychanged); #endregion * * Note: The above 3 tests, change the value in the text box, the corresponding property values in the data source will change * However, the value of the data source changed, the value in the text box will not change the * * * #reg
      Ion Test four: list<t> blognews = new list<blognew> ();
      Blognews.add (new Blognew {BlogID = 1, Blogtitle = "life if only as first sight"});
      Blognews.add (new Blognew {BlogID = 2, Blogtitle = "What Autumn sad Painting Fan"}); Blognews.add (new Blognew {BlogID = 3, BlOgtitle = "Most like the" most of the "de"});
      DATAGRIDVIEW1.DATABINDINGS.ADD ("DataSource", this, "Blognews", false, datasourceupdatemode.onpropertychanged);
      #endregion #region Test Five: bindinglist<t> Blognewsregardui = new bindinglist<blognew> ();
      Blognewsregardui.add (new Blognew {BlogID = one, Blogtitle = "Zombie Lonely Village Not Self-pity"});
      Blognewsregardui.add (new Blognew {BlogID =, Blogtitle = "Still think for the national Garrison Luntai"});
      Blognewsregardui.add (new Blognew {BlogID = +, Blogtitle = "night diaphragm listening to Wind blowing"}); DATAGRIDVIEW2.DATABINDINGS.ADD ("DataSource", this, "Blognewsregardui", false,
      datasourceupdatemode.onpropertychanged); #endregion} private void Button1_Click (object sender, EventArgs e) {//From here you can see that the values in TextBox2 have changed, and the values here have changed, The reason is because the class belongs to the reference type MessageBox.Show (myDataSource).
    MyValue); } private void Button2_Click (object sender, EventArgs e) {//From here you can see that the value in TextBox3 has changed, and the value here has changed,//the reason is num is treated as a property of the current form (the form itself is a class) and also belongs to the reference type MessageBox.Show (Num.tostriNg ()); This.
      Num = 10;
    MessageBox.Show (Num.tostring ()); } private void Button3_Click (object sender, EventArgs e) {//Insert a row of VAR data into datagridview here = DataGrid
      View1.datasource as list<blognew>; Data.
      ADD (new Blognew {BlogID = 4, Blogtitle = "Take a look at the lazy flower, half edge of the monastic half June"}); foreach (Blognew blognew in Datagridview1.datasource as list<blognew>) {/*********** * when our hearts plug In a blogID record of 4 data, the interface can be seen DataGridView1 datasource has been updated, * But the interface is still displayed as blogID 1,2,3 three data, very strange * **********
      /MessageBox.Show (Blognew.blogid + "--" + blognew.blogtitle); The private void Button4_Click (object sender, EventArgs e) {*/* is mainly used to solve the problem of not updating the DataGridView1 interface, in fact, because of the use of Li St<blognew&gt, here we use bindlist<blognew> * through testing, we found that as long as the data source changes, the interface can be automatically updated, is very convenient, do not need to rebind/var D
      Ataregardui = Datagridview2.datasource as bindinglist<blognew>; Dataregardui.add (New Blognew { BlogID =, Blogtitle = "Bamboo peach blossom 32, Spring River Plumbing Duck Prophet"});
  } public class myDataSource {public string myvalue {get; set;}
    public class Blognew {public int BlogID {get; set;}
  public string Blogtitle {get; set;}

 }
}

Form2.Designer.cs Code:

Namespace Databindingstest {Partial class Form2 {///<summary>///Required Designer.
    </summary> private System.ComponentModel.IContainer components = null;
    <summary>///clean up any being used. </summary>///<param name= "disposing" >true if managed resources should is disposed; Otherwise, false.</param> protected override void Dispose (bool disposing) {if disposing && ( Components!= null)) {components.
      Dispose (); Base.
    Dispose (disposing); #region Windows Form Designer generated code///<summary>///Required method for Designer Support-
    Do not modify///the contents of this method with the Code editor. </summary> private void InitializeComponent () {this.groupbox1 = new System.Windows.Forms.GroupBox
      ();
      This.groupbox2 = new System.Windows.Forms.GroupBox (); This.button1 = new System.Windows.Forms.Button ();
      This.textbox2 = new System.Windows.Forms.TextBox ();
      This.groupbox3 = new System.Windows.Forms.GroupBox ();
      This.button2 = new System.Windows.Forms.Button ();
      This.textbox3 = new System.Windows.Forms.TextBox ();
      This.groupbox4 = new System.Windows.Forms.GroupBox ();
      This.button3 = new System.Windows.Forms.Button ();
      This.datagridview1 = new System.Windows.Forms.DataGridView ();
      THIS.GROUPBOX5 = new System.Windows.Forms.GroupBox ();
      This.button4 = new System.Windows.Forms.Button ();
      This.datagridview2 = new System.Windows.Forms.DataGridView ();
      This.textbox1 = new System.Windows.Forms.TextBox ();
      THIS.TRACKBAR1 = new System.Windows.Forms.TrackBar ();
      This.groupBox1.SuspendLayout ();
      This.groupBox2.SuspendLayout ();
      This.groupBox3.SuspendLayout ();
      This.groupBox4.SuspendLayout (); ((System.ComponentModel.ISupportInitialize) (This.datagridview1)).
 BeginInit ();     This.groupBox5.SuspendLayout (); ((System.ComponentModel.ISupportInitialize) (THIS.DATAGRIDVIEW2)).
      BeginInit (); ((System.ComponentModel.ISupportInitialize) (THIS.TRACKBAR1)).
      BeginInit (); This.
      SuspendLayout ();
      GroupBox1//THIS.GROUPBOX1.CONTROLS.ADD (THIS.TRACKBAR1);
      THIS.GROUPBOX1.CONTROLS.ADD (This.textbox1);
      This.groupBox1.Location = new System.Drawing.Point (12, 12);
      This.groupBox1.Name = "GroupBox1";
      This.groupBox1.Size = new System.Drawing.Size (200, 100);
      This.groupBox1.TabIndex = 0;
      This.groupBox1.TabStop = false;
      This.groupBox1.Text = "mode One";
      GroupBox2//THIS.GROUPBOX2.CONTROLS.ADD (This.button1);
      THIS.GROUPBOX2.CONTROLS.ADD (THIS.TEXTBOX2);
      This.groupBox2.Location = new System.Drawing.Point (218, 12);
      This.groupBox2.Name = "GroupBox2";
      This.groupBox2.Size = new System.Drawing.Size (200, 100);
      This.groupBox2.TabIndex = 2; ThiS.groupbox2.tabstop = false;
      This.groupBox2.Text = "mode Two";
      Button1//this.button1.Location = new System.Drawing.Point (22, 59);
      This.button1.Name = "Button1";
      This.button1.Size = new System.Drawing.Size (157, 23);
      This.button1.TabIndex = 3;
      This.button1.Text = "View the value of the modified data source";
      This.button1.UseVisualStyleBackColor = true;
      This.button1.Click + = new System.EventHandler (This.button1_click);
      TextBox2//this.textBox2.Location = new System.Drawing.Point (22, 20);
      This.textBox2.Name = "TextBox2";
      This.textBox2.Size = new System.Drawing.Size (157, 21);
      This.textBox2.TabIndex = 2;
      GroupBox3//THIS.GROUPBOX3.CONTROLS.ADD (This.button2);
      THIS.GROUPBOX3.CONTROLS.ADD (THIS.TEXTBOX3);
      This.groupBox3.Location = new System.Drawing.Point (428, 12);
      This.groupBox3.Name = "GroupBox3"; This.groupBox3.Size = new System.Drawing.Size (200,100);
      This.groupBox3.TabIndex = 4;
      This.groupBox3.TabStop = false;
      This.groupBox3.Text = "mode three";
      Button2//this.button2.Location = new System.Drawing.Point (22, 59);
      This.button2.Name = "Button2";
      This.button2.Size = new System.Drawing.Size (157, 23);
      This.button2.TabIndex = 3;
      This.button2.Text = "View the value of the modified data source";
      This.button2.UseVisualStyleBackColor = true;
      This.button2.Click + = new System.EventHandler (This.button2_click);
      TextBox3//this.textBox3.Location = new System.Drawing.Point (22, 20);
      This.textBox3.Name = "TextBox3";
      This.textBox3.Size = new System.Drawing.Size (157, 21);
      This.textBox3.TabIndex = 2;
      GROUPBOX4//THIS.GROUPBOX4.CONTROLS.ADD (THIS.BUTTON3);
      THIS.GROUPBOX4.CONTROLS.ADD (THIS.DATAGRIDVIEW1);
      This.groupBox4.Location = new System.Drawing.Point (12, 118);
  This.groupBox4.Name = "GroupBox4";    This.groupBox4.Size = new System.Drawing.Size (568, 157);
      This.groupBox4.TabIndex = 5;
      This.groupBox4.TabStop = false;
      This.groupBox4.Text = "Mode IV";
      Button3//this.button3.Location = new System.Drawing.Point (377, 122);
      This.button3.Name = "Button3";
      This.button3.Size = new System.Drawing.Size (157, 23);
      This.button3.TabIndex = 4;
      This.button3.Text = "Insert Row";
      This.button3.UseVisualStyleBackColor = true;
      This.button3.Click + = new System.EventHandler (This.button3_click); DataGridView1//This.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridV
      Iewcolumnheadersheightsizemode.autosize;
      This.dataGridView1.Location = new System.Drawing.Point (18, 20);
      This.dataGridView1.Name = "DataGridView1";
      This.dataGridView1.RowTemplate.Height = 23;
      This.dataGridView1.Size = new System.Drawing.Size (516, 96); This.dataGridView1.TabIndex =0;
      GROUPBOX5//THIS.GROUPBOX5.CONTROLS.ADD (THIS.BUTTON4);
      THIS.GROUPBOX5.CONTROLS.ADD (THIS.DATAGRIDVIEW2);
      This.groupBox5.Location = new System.Drawing.Point (12, 281);
      This.groupBox5.Name = "GROUPBOX5";
      This.groupBox5.Size = new System.Drawing.Size (568, 162);
      This.groupBox5.TabIndex = 6;
      This.groupBox5.TabStop = false;
      This.groupBox5.Text = "mode Five";
      Button4//this.button4.Location = new System.Drawing.Point (377, 127);
      This.button4.Name = "Button4";
      This.button4.Size = new System.Drawing.Size (157, 23);
      This.button4.TabIndex = 4;
      This.button4.Text = "Insert Row";
      This.button4.UseVisualStyleBackColor = true;
      This.button4.Click + = new System.EventHandler (This.button4_click); DATAGRIDVIEW2//This.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridV
      Iewcolumnheadersheightsizemode.autosize; This.dataGridview2.location = new System.Drawing.Point (18, 20);
      This.dataGridView2.Name = "DataGridView2";
      This.dataGridView2.RowTemplate.Height = 23;
      This.dataGridView2.Size = new System.Drawing.Size (516, 91);
      This.dataGridView2.TabIndex = 0;
      TextBox1//this.textBox1.Location = new System.Drawing.Point (18, 20);
      This.textBox1.Name = "TextBox1";
      This.textBox1.Size = new System.Drawing.Size (157, 21);
      This.textBox1.TabIndex = 0;
      TRACKBAR1//this.trackBar1.Location = new System.Drawing.Point (18, 47);
      This.trackBar1.Name = "TrackBar1";
      This.trackBar1.Size = new System.Drawing.Size (157, 45);
      This.trackBar1.TabIndex = 1; Form2//this.
      Autoscaledimensions = new System.Drawing.SizeF (6F, 12F); This.
      AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; This.
      ClientSize = new System.Drawing.Size (676, 471); This. Controls.Add (this.grOUPBOX5); This.
      Controls.Add (THIS.GROUPBOX4); This.
      Controls.Add (THIS.GROUPBOX3); This.
      Controls.Add (THIS.GROUPBOX2); This.
      Controls.Add (This.groupbox1); This.
      Name = "Form2"; This.
      Text = "Form2"; This.
      Load + = new System.EventHandler (this.mainfrm_load);
      This.groupBox1.ResumeLayout (FALSE);
      This.groupBox1.PerformLayout ();
      This.groupBox2.ResumeLayout (FALSE);
      This.groupBox2.PerformLayout ();
      This.groupBox3.ResumeLayout (FALSE);
      This.groupBox3.PerformLayout ();
      This.groupBox4.ResumeLayout (FALSE); ((System.ComponentModel.ISupportInitialize) (This.datagridview1)).
      EndInit ();
      This.groupBox5.ResumeLayout (FALSE); ((System.ComponentModel.ISupportInitialize) (THIS.DATAGRIDVIEW2)).
      EndInit (); ((System.ComponentModel.ISupportInitialize) (THIS.TRACKBAR1)).
      EndInit (); This.
    ResumeLayout (FALSE);
    #endregion private System.Windows.Forms.GroupBox GroupBox1; Private System.Windows.Forms.GrouPbox GroupBox2;
    Private System.Windows.Forms.Button button1;
    Private System.Windows.Forms.TextBox TextBox2;
    Private System.Windows.Forms.GroupBox GroupBox3;
    Private System.Windows.Forms.Button button2;
    Private System.Windows.Forms.TextBox TextBox3;
    Private System.Windows.Forms.GroupBox groupBox4;
    Private System.Windows.Forms.Button Button3;
    Private System.Windows.Forms.DataGridView DataGridView1;
    Private System.Windows.Forms.GroupBox groupBox5;
    Private System.Windows.Forms.Button Button4;
    Private System.Windows.Forms.DataGridView dataGridView2;
    Private System.Windows.Forms.TrackBar trackBar1;
  Private System.Windows.Forms.TextBox TextBox1;

 }
}

Effect Chart:

Read more about C # Interested readers can view the site topics: "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # object-oriented Program design Introductory Course" and "C # programming Thread Usage Skill Summary"

I hope this article will help you with C # programming.

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.