最近剛接觸到資料繫結,以前在學校時一直自己寫代碼來關聯資料,當時沒有太多心思去看這方面的東西,所以一直認為用到了資料的綁定就會降低我們對資料的控制力。然而當時寫出來的東西又麻煩又難於管理,還會經常出錯,蓋了這個又影響到了那個。到了公司,在同事指導下,看了一點關於資料繫結方面的東西。發現有以下好處:一是,實現了程式中的特定的功能;二是,非常易於管理;三是,靈活性也沒有受到太大影響,很多東西都是能很好的綁定起來,只是我們不知道怎麼做。(至少現在我用到的地方,靈活性沒有受到太多影響)。寫此篇劄記也為我以後存根,便於查看。
1、 ComboBox的綁定
將 DataSource 屬性設定為某個資料來源對象。可能的資料來源包括資料表、資料檢視、資料集、資料檢視管理器、數組或實現了 IList 介面的任何類。
將 DisplayMember 和 ValueMember 分別綁定到資料來源的某一列上,例如:
this.form.comboBox5.DataSource = this.manager.GetBillActionGroup().Tables[0];
this.form.comboBox5.DisplayMember = "GROUPNAME";
this.form.comboBox5.ValueMember = "GROUPID";
這樣在介面上看到的將是displaymember中制定的資料,而在取selectvalue時則是會取到valuemember中制定的資料。
2、 將ComboBox綁定到實現了 IList 介面的類
下面是我寫的一個完整的例子,用於將一個有兩個屬性的country類綁定到comboBox,displaymember綁到shortname,valuemember綁到這個對象。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TestBanding
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 表單設計器產生的程式碼
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(96, 104);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 40);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
CountryCollection cc = CreateData();
this.comboBox1.DataSource = cc;
this.comboBox1.DisplayMember = "CountryCode";
this.comboBox1.ValueMember = "MyCountry";
}
private CountryCollection CreateData()
{
CountryCollection countryCollection = new CountryCollection();
Country c1 = new Country("PRC","China");
countryCollection.Add(c1);
Country c2 = new Country("Ame","America");
countryCollection.Add(c2);
Country c3 = new Country("Eng","England");
countryCollection.Add(c3);
return countryCollection;
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.textBox1.Text = ((Country)this.comboBox1.SelectedValue).ShortName;
}
}
public class CountryCollection : CollectionBase
{
public void Add(Country country)
{
this.List.Add(country);
}
public virtual Country this[int index]
{
get
{
if(index>=0 || index<this.List.Count)
{
return (Country)this.List[index];
}
else
{
throw new ArgumentOutOfRangeException(index+"");
}
}
set
{
if(index>=0 || index<this.List.Count)
{
if(value.GetType() == typeof(Country))
{
this.List[index] = value;
}
else
{
throw new Exception("Wrong type!");
}
}
else
{
throw new ArgumentOutOfRangeException(index+"");
}
}
}
}
public class Country
{
public Country(string code,string sName)
{
this.countryCode = code;
this.shortName = sName;
}
private string countryCode;
private string shortName;
public string CountryCode{get{return this.countryCode;}set{this.countryCode=value;}}
public string ShortName{get{return this.shortName;}set{this.shortName=value;}}
public Country MyCountry{get{return this;}}
}
}
3、 卡片上的資料繫結
我們經常會做這樣的東西,左邊一個datagrid,右邊一堆textbox和combobox之類的東西,要選中datagrid中的行來用右邊的卡片來顯示具體的明細。原來我都是手寫的代碼,維護起來非常的麻煩。後來發現,只要將右邊的卡片中的控制項也綁定到和datagrid相同資料來源,在datagrid中選中行,在右邊的卡片上就能切換不同的資料,非常方便。卡片控制項的綁定如下:
textBox1.DataBindings.Add("Text", myTable, "customID");
4、 資料導航條
我們可能會用到資料導航條,雖然不太常用。記得在msdn中的一個樣本中,就有可以通過嚮導的方式來建立這種資料導航條。
主要我們要注意的類是CurrencyManager,它可以管理綁定的對象,其實是封裝了對綁定資料來源的統一的定位方式,通過下面的方法添加綁定:
myCurrencyManager = (CurrencyManager)this.BindingContext[myTable];
通過下面的方法定位元據源:
myCurrencyManager.Position = 0;
就到這吧,本人水平有限,只能寫出這麼多了,最後推薦大家一個好論壇,
http://forums.microsoft.com/msdn/default.aspx?ForumGroupID=2,這塊是專門討論winform的。
還有幫我點旁邊的google廣告,給我留言。