C#中DataBindings用法實例剖析。本站提示廣大學習愛好者:(C#中DataBindings用法實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中DataBindings用法實例剖析正文
本文實例講述了C#中DataBindings用法。分享給年夜家供年夜家參考,詳細以下:
在C#操作數據庫進程中,針對普通的文本控件,好比TextBox,Label等,我們賦值直接應用相似TextBox.Text=****的方法來停止,這類方法從某種意義下去說切實其實是最輕便的方法,然則關於龐雜一些的空間,好比說DataGridView,這個時刻,綁定命據源我們普通應用DataGridView1.DataSource=****的方法來停止,假如數據源略微有更改,那末只須要從新挪用綁定一遍便可。可以說這類方法是單向的,也即從數據庫到UI,然則有無一種方法可以或許完成數據源轉變的時刻,不消從新綁定DataGridView就讓它可以或許主動刷新數據呢,固然,這裡要提到的就是DataBinding了。
代碼以下
Form2.cs代碼:
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(); //運用於第二種方法 public int Num { get; set; } //運用於第三種方法 public List<BlogNew> blogNews { get; set; } //運用於第四種方法 public BindingList<BlogNew> blogNewsRegardUI { get; set; } //運用於DataGridView界面UI更新 private void mainFrm_Load(object sender, EventArgs e) { #region 測試一 /************************************************ * 第一個值:要綁定到TextBox的甚麼處所 * 第二個值:數據源是甚麼 * 第三個值:應當取數據源的甚麼屬性 * 第四個值:能否開啟數據格局化 * 第五個值:在甚麼時刻啟用數據源綁定 * *********************************************/ textBox1.DataBindings.Add("Text", trackBar1, "Value", false, DataSourceUpdateMode.OnPropertyChanged); #endregion #region 測試二 /********************************************* * 這個重要就是經由過程一個內部的類,當作數據源 * *********************************************/ mydatasource.Myvalue = "這是個測試"; textBox2.DataBindings.Add("Text", mydatasource, "Myvalue", false, DataSourceUpdateMode.OnPropertyChanged); #endregion #region 測試三 /***************************************** *這個重要就是經由過程自己具有的屬性,當作數據源 ****************************************/ Num = 5; textBox3.DataBindings.Add("Text", this, "Num", false, DataSourceUpdateMode.OnPropertyChanged); #endregion /* * 留意:下面的3個測試,轉變文本框中的值,數據源中對應的屬性值會轉變 * 然則,數據源的屬性值轉變了,文本框中的值不會轉變 */ #region 測試四 : List<T> blogNews = new List<BlogNew>(); blogNews.Add(new BlogNew { BlogID = 1, BlogTitle = "人生若只如初見" }); blogNews.Add(new BlogNew { BlogID = 2, BlogTitle = "何事金風抽豐悲畫扇" }); blogNews.Add(new BlogNew { BlogID = 3, BlogTitle = "最愛好納蘭性德" }); dataGridView1.DataBindings.Add("DataSource", this, "blogNews", false, DataSourceUpdateMode.OnPropertyChanged); #endregion #region 測試五 : BindingList<T> blogNewsRegardUI = new BindingList<BlogNew>(); blogNewsRegardUI.Add(new BlogNew { BlogID = 11, BlogTitle = "僵臥孤村不自哀" }); blogNewsRegardUI.Add(new BlogNew { BlogID = 12, BlogTitle = "尚思為國戍輪台" }); blogNewsRegardUI.Add(new BlogNew { BlogID = 13, BlogTitle = "夜闌臥聽風吹雨" }); dataGridView2.DataBindings.Add("DataSource", this, "blogNewsRegardUI", false, DataSourceUpdateMode.OnPropertyChanged); #endregion } private void button1_Click(object sender, EventArgs e) { //從這裡可以看出,轉變了TextBox2中的值,這裡的值也轉變了,緣由是由於類屬於援用類型 MessageBox.Show(mydatasource.Myvalue); } private void button2_Click(object sender, EventArgs e) { //從這裡可以看出,轉變了TextBox3中的值,這裡的值也轉變了, //緣由是Num被當作了以後窗體的一個屬性(窗體自己就是一個類),也屬於援用類型 MessageBox.Show(Num.ToString()); //this.Num = 10; //MessageBox.Show(Num.ToString()); } private void button3_Click(object sender, EventArgs e) { //在這裡向DataGridView中拔出一行 var data = dataGridView1.DataSource as List<BlogNew>; data.Add(new BlogNew { BlogID = 4, BlogTitle = "取次花叢懶回想,半緣修道半緣君" }); foreach (BlogNew blogNew in dataGridView1.DataSource as List<BlogNew>) { /*********** * 當我們心拔出一條BlogID記載為4的數據的時刻,在界面上可以看出dataGridView1的dataSource曾經被更新, * 然則界面上照舊顯示為BlogID為1,2,3三條數據,很奇異 * *********************/ MessageBox.Show(blogNew.BlogID + "--" + blogNew.BlogTitle); } } private void button4_Click(object sender, EventArgs e) { /*這裡重要用來處理DataGridView1界面不更新的成績,其實緣由在於應用了List<BlogNew>,這裡我們采取BindList<BlogNew> *經由過程測試,我們發明,只需數據源轉變,界面便可以主動的停止更新了,很是便利,不須要從新綁定 */ var dataRegardUI = dataGridView2.DataSource as BindingList<BlogNew>; dataRegardUI.Add(new BlogNew { BlogID = 20, BlogTitle = "竹外桃花三兩枝,春江水暖鴨先知" }); } } public class MyDataSource { public string Myvalue { get; set; } } public class BlogNew { public int BlogID { get; set; } public string BlogTitle { get; set; } } }
Form2.Designer.cs代碼:
namespace DataBindingsTest { partial class Form2 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be 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 = "方法一"; // // 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 = "方法二"; // // 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 = "檢查已修正的數據源的值"; 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 = "方法三"; // // 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 = "檢查已修正的數據源的值"; 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 = "方法四"; // // 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 = "拔出一行"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.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 = "方法五"; // // 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 = "拔出一行"; this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); // // dataGridView2 // this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.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; } }
後果圖:
願望本文所述對年夜家C#法式設計有所贊助。