现在的位置: 首页 > 综合 > 正文

关于数据绑定

2013年02月08日 ⁄ 综合 ⁄ 共 4953字 ⁄ 字号 评论关闭
 

    最近刚接触到数据绑定,以前在学校时一直自己写代码来关联数据,当时没有太多心思去看这方面的东西,所以一直认为用到了数据的绑定就会降低我们对数据的控制力。然而当时写出来的东西又麻烦又难于管理,还会经常出错,盖了这个又影响到了那个。到了公司,在同事指导下,看了一点关于数据绑定方面的东西。发现有以下好处:一是,实现了程序中的特定的功能;二是,非常易于管理;三是,灵活性也没有受到太大影响,很多东西都是能很好的绑定起来,只是我们不知道怎么做。(至少现在我用到的地方,灵活性没有受到太多影响)。写此篇札记也为我以后存根,便于查看。

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,右边一堆textboxcombobox之类的东西,要选中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广告,给我留言。

抱歉!评论已关闭.