C#中DataBindings用法實(shí)例分析
本文實(shí)例講述了C#中DataBindings用法。分享給大家供大家參考,具體如下:
在C#操作數(shù)據(jù)庫(kù)過(guò)程中,針對(duì)一般的文本控件,比如TextBox,Label等,我們賦值直接使用類(lèi)似TextBox.Text=****的方式來(lái)進(jìn)行,這種方式從某種意義上來(lái)說(shuō)的確是最簡(jiǎn)便的方式,但是對(duì)于復(fù)雜一些的空間,比如說(shuō)DataGridView,這個(gè)時(shí)候,綁定數(shù)據(jù)源我們一般使用DataGridView1.DataSource=****的方式來(lái)進(jìn)行,如果數(shù)據(jù)源稍微有更改,那么只需要重新調(diào)用綁定一遍即可??梢哉f(shuō)這種方式是單向的,也即從數(shù)據(jù)庫(kù)到UI,但是有沒(méi)有一種方式能夠?qū)崿F(xiàn)數(shù)據(jù)源改變的時(shí)候,不用重新綁定DataGridView就讓它能夠自動(dòng)刷新數(shù)據(jù)呢,當(dāng)然,這里要提到的就是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(); //應(yīng)用于第二種方式
public int Num { get; set; } //應(yīng)用于第三種方式
public List<BlogNew> blogNews { get; set; } //應(yīng)用于第四種方式
public BindingList<BlogNew> blogNewsRegardUI { get; set; } //應(yīng)用于DataGridView界面UI更新
private void mainFrm_Load(object sender, EventArgs e)
{
#region 測(cè)試一
/************************************************
* 第一個(gè)值:要綁定到TextBox的什么地方
* 第二個(gè)值:數(shù)據(jù)源是什么
* 第三個(gè)值:應(yīng)該取數(shù)據(jù)源的什么屬性
* 第四個(gè)值:是否開(kāi)啟數(shù)據(jù)格式化
* 第五個(gè)值:在什么時(shí)候啟用數(shù)據(jù)源綁定
* *********************************************/
textBox1.DataBindings.Add("Text", trackBar1, "Value", false, DataSourceUpdateMode.OnPropertyChanged);
#endregion
#region 測(cè)試二
/*********************************************
* 這個(gè)主要就是通過(guò)一個(gè)外部的類(lèi),當(dāng)做數(shù)據(jù)源
* *********************************************/
mydatasource.Myvalue = "這是個(gè)測(cè)試";
textBox2.DataBindings.Add("Text", mydatasource, "Myvalue", false, DataSourceUpdateMode.OnPropertyChanged);
#endregion
#region 測(cè)試三
/*****************************************
*這個(gè)主要就是通過(guò)本身?yè)碛械膶傩?,?dāng)做數(shù)據(jù)源
****************************************/
Num = 5;
textBox3.DataBindings.Add("Text", this, "Num", false, DataSourceUpdateMode.OnPropertyChanged);
#endregion
/*
* 注意:上面的3個(gè)測(cè)試,改變文本框中的值,數(shù)據(jù)源中對(duì)應(yīng)的屬性值會(huì)改變
* 但是,數(shù)據(jù)源的屬性值改變了,文本框中的值不會(huì)改變
*/
#region 測(cè)試四 : List<T>
blogNews = new List<BlogNew>();
blogNews.Add(new BlogNew { BlogID = 1, BlogTitle = "人生若只如初見(jiàn)" });
blogNews.Add(new BlogNew { BlogID = 2, BlogTitle = "何事秋風(fēng)悲畫(huà)扇" });
blogNews.Add(new BlogNew { BlogID = 3, BlogTitle = "最喜歡納蘭性德" });
dataGridView1.DataBindings.Add("DataSource", this, "blogNews", false, DataSourceUpdateMode.OnPropertyChanged);
#endregion
#region 測(cè)試五 : BindingList<T>
blogNewsRegardUI = new BindingList<BlogNew>();
blogNewsRegardUI.Add(new BlogNew { BlogID = 11, BlogTitle = "僵臥孤村不自哀" });
blogNewsRegardUI.Add(new BlogNew { BlogID = 12, BlogTitle = "尚思為國(guó)戍輪臺(tái)" });
blogNewsRegardUI.Add(new BlogNew { BlogID = 13, BlogTitle = "夜闌臥聽(tīng)風(fēng)吹雨" });
dataGridView2.DataBindings.Add("DataSource", this, "blogNewsRegardUI", false, DataSourceUpdateMode.OnPropertyChanged);
#endregion
}
private void button1_Click(object sender, EventArgs e)
{
//從這里可以看出,改變了TextBox2中的值,這里的值也改變了,原因是因?yàn)轭?lèi)屬于引用類(lèi)型
MessageBox.Show(mydatasource.Myvalue);
}
private void button2_Click(object sender, EventArgs e)
{
//從這里可以看出,改變了TextBox3中的值,這里的值也改變了,
//原因是Num被當(dāng)做了當(dāng)前窗體的一個(gè)屬性(窗體本身就是一個(gè)類(lèi)),也屬于引用類(lèi)型
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>)
{
/***********
* 當(dāng)我們心插入一條BlogID記錄為4的數(shù)據(jù)的時(shí)候,在界面上可以看出dataGridView1的dataSource已經(jīng)被更新,
* 但是界面上依舊顯示為BlogID為1,2,3三條數(shù)據(jù),很奇怪
* *********************/
MessageBox.Show(blogNew.BlogID + "--" + blogNew.BlogTitle);
}
}
private void button4_Click(object sender, EventArgs e)
{
/*這里主要用來(lái)解決DataGridView1界面不更新的問(wèn)題,其實(shí)原因在于使用了List<BlogNew>,這里我們采用BindList<BlogNew>
*通過(guò)測(cè)試,我們發(fā)現(xiàn),只要數(shù)據(jù)源改變,界面就可以自動(dòng)的進(jìn)行更新了,很是方便,不需要重新綁定
*/
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 = "查看已修改的數(shù)據(jù)源的值";
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 = "查看已修改的數(shù)據(jù)源的值";
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;
}
}
效果圖:
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
上一篇:C#中事件的定義和使用
欄 目:C#教程
下一篇:C#中使用基數(shù)排序算法對(duì)字符串進(jìn)行排序的示例
本文標(biāo)題:C#中DataBindings用法實(shí)例分析
本文地址:http://m.jygsgssxh.com/a1/C_jiaocheng/6463.html
您可能感興趣的文章
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并打開(kāi)的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10Extjs4如何處理后臺(tái)json數(shù)據(jù)中日期和時(shí)間
- 01-10C#中DataGridView常用操作實(shí)例小結(jié)
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10C#線程隊(duì)列用法實(shí)例分析
- 01-10C#利用反射技術(shù)實(shí)現(xiàn)去掉按鈕選中時(shí)的邊框效果
- 01-10C#中查找Dictionary中的重復(fù)值的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改


