亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

C#中DataBindings用法實例分析

2020-01-24 01:07:32
字體:
來源:轉載
供稿:網友

本文實例講述了C#中DataBindings用法。分享給大家供大家參考,具體如下:

在C#操作數據庫過程中,針對一般的文本控件,比如TextBox,Label等,我們賦值直接使用類似TextBox.Text=****的方式來進行,這種方式從某種意義上來說的確是最簡便的方式,但是對于復雜一些的空間,比如說DataGridView,這個時候,綁定數據源我們一般使用DataGridView1.DataSource=****的方式來進行,如果數據源稍微有更改,那么只需要重新調用綁定一遍即可??梢哉f這種方式是單向的,也即從數據庫到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#相關內容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數據結構與算法教程》、《C#面向對象程序設計入門教程》及《C#程序設計之線程使用技巧總結

希望本文所述對大家C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久久精品影院| 久久影院资源网| 黄网动漫久久久| 精品国产91久久久| 97精品视频在线观看| 亚洲欧美国产制服动漫| 日韩av在线不卡| 91免费欧美精品| 国产91在线播放精品91| 91免费人成网站在线观看18| 91久久夜色精品国产网站| 在线看片第一页欧美| 欧美专区在线播放| 欧美国产日韩一区二区在线观看| 欧美精品18videos性欧美| 国产精品久久久久免费a∨大胸| 一本色道久久综合狠狠躁篇怎么玩| 91久久国产综合久久91精品网站| 欧美激情高清视频| 欧美韩日一区二区| 欧美视频在线免费| 最近更新的2019中文字幕| 亚洲a级在线播放观看| 97在线精品视频| 亚洲免费高清视频| www亚洲精品| 97碰碰碰免费色视频| 欧美精品在线观看91| 精品国产乱码久久久久久婷婷| 亚州国产精品久久久| 国产99久久精品一区二区永久免费| 欧美老女人在线视频| 一区二区三区高清国产| 97色伦亚洲国产| 欧美日韩另类在线| 欧美日韩国产一中文字不卡| 欧美激情免费看| 亚洲国产精品成人一区二区| 91av视频导航| 欧美性jizz18性欧美| **欧美日韩vr在线| 国产一区二区三区免费视频| 日韩中文字幕免费看| 久久夜精品香蕉| 精品视频一区在线视频| 91最新国产视频| 激情成人中文字幕| 国产91精品久久久久| 久久久久久国产精品三级玉女聊斋| 精品中文视频在线| 亚洲精品久久在线| 欧美插天视频在线播放| 亚洲国产精品久久久久秋霞蜜臀| 欧美精品精品精品精品免费| 欧美性jizz18性欧美| 性欧美办公室18xxxxhd| 中文国产成人精品久久一| 久久亚洲国产精品成人av秋霞| www.欧美精品一二三区| 国产精品第七影院| 富二代精品短视频| 久久精品国产欧美亚洲人人爽| 亚洲成人在线网| 精品少妇v888av| 中文字幕国产亚洲| 欧美精品第一页在线播放| 国产一区二区丝袜| 国产精品小说在线| 欧美视频中文字幕在线| 亚洲丁香婷深爱综合| 成人激情视频在线| 91国语精品自产拍在线观看性色| 米奇精品一区二区三区在线观看| 亚洲人成网站色ww在线| 国产精品盗摄久久久| 欧美午夜久久久| 久久99久久99精品免观看粉嫩| 国产亚洲精品美女久久久| 亚洲精品电影在线观看| 97精品久久久| 91久久在线播放| 欧洲成人免费aa| 日韩影视在线观看| 91在线免费观看网站| 国产欧美一区二区三区视频| 亚洲剧情一区二区| 琪琪第一精品导航| 亚洲网站视频福利| 日韩一区在线视频| 欧美精品在线免费播放| 国产中文日韩欧美| 91美女高潮出水| 国产欧亚日韩视频| 欧美日产国产成人免费图片| 国产亚洲欧美aaaa| 一区二区三区 在线观看视| 日韩一区二区久久久| 成人福利视频网| 欧美性猛交xxxxx免费看| 好吊成人免视频| 久久久av电影| 久久精品视频免费播放| 国产一区二区丝袜高跟鞋图片| 国产午夜精品免费一区二区三区| 日韩av在线影视| 激情成人在线视频| 精品伊人久久97| 久久久最新网址| 国产精品亚洲美女av网站| 日本道色综合久久影院| 久久99亚洲精品| 国产日韩一区在线| 欧美又大粗又爽又黄大片视频| 国产精品无码专区在线观看| 伊人激情综合网| 亚洲精品视频网上网址在线观看| 欧美日韩精品国产| 最近免费中文字幕视频2019| 亚洲综合社区网| 欧美久久久精品| 亚洲jizzjizz日本少妇| 亚洲国产成人精品女人久久久| 欧美日韩国产91| 日韩欧美精品网站| 国产精品无码专区在线观看| 国产精品jizz在线观看麻豆| 久久久久久免费精品| 日韩高清电影免费观看完整版| 亚洲第一精品电影| 欧美极品在线视频| 在线精品国产成人综合| 国产精品露脸自拍| 免费成人高清视频| 成人免费高清完整版在线观看| 日韩国产精品亚洲а∨天堂免| 九九久久国产精品| 亚洲国产黄色片| 18性欧美xxxⅹ性满足| 国产亚洲欧美日韩美女| 成人激情视频小说免费下载| 韩国19禁主播vip福利视频| 亚洲色图50p| 国产精品影院在线观看| 国产精品久久激情| 久久精品99久久香蕉国产色戒| 国产91色在线播放| 欧美三级欧美成人高清www| 91免费国产网站| 一区二区三区黄色| 日韩亚洲精品电影| 国产精品久久久久久久av大片| 亚洲视屏在线播放| 久久九九免费视频| 亚洲老头老太hd| 亚洲国产欧美自拍| 欧美国产日本高清在线| 国产精品久久久久久久av电影| 国产精品欧美亚洲777777| 欧美精品电影免费在线观看| 九九热这里只有精品免费看| 国产精品一区二区久久久| 欧美午夜影院在线视频| 欧美三级欧美成人高清www| 亚洲黄色av网站|