TextBox控件的DataBindings屬性


DataBindings屬性是很多控件都有的屬性,作用有2方面。一方面是用於與數據庫的數據進行綁定,進行數據顯示。另一方面用於與控件或類的對象進行數據綁定。這里主要關注后者。主要用法是將某個對象的某個屬性與指定對象的指定屬性進行關聯.

Label、TextBox等都包含DataBindings屬性,其類型為ControlBindingsCollection,是Binding類的集合。Binding類代表某對象屬性值和某控件屬性值之間的簡單綁定。如可以將TextBox的Text屬性值綁定到Label的Text屬性值,這樣,當TextBox中的文本被修改的時候,Label的文本也會及時進行修改,如下面的代碼所示:

Label1.DataBindings.Add("Text",TextBox1,"Text");

Binding類除了可以將對象的屬性綁定到控件的屬性之外,還可以將對象列表中當前對象的屬性值綁定到控件的屬性。

當使用Binding的構造函數創建實例時,必須指定三項內容:

  • 要綁定到的控件屬性的名稱
  • 數據源
  • 數據源中解析為列表或屬性的導航路徑

其中,數據源可以為:

  • 實現 IBindingList 或 ITypedList 的任何類。包括:DataSet、DataTable、DataView 或 DataViewManager。 
  • 實現 IList 的任意索引集合類。(必須在創建 Binding 之前創建和填充該集合,並且列表中的所有對象必須為同一類型,否則將引發異常) 
  • 強類型對象的強類型 IList。

導航路徑可以為空字符串(默認將調用數據源的ToString()方法)、單個屬性名稱或用點分隔的名稱層次結構。

名稱層次結構是什么意思呢?比如我們有一個Company類,它包含Name屬性和Employees屬性(公司所有Employee的集合),而Employee類又包含Name屬性。那么,如果要將Company的Name屬性綁定到TextBox控件的Text屬性,代碼為:

TextBox1.DataBindings.Add("Text", company, "Name");

如果要綁定Employees的Name屬性,代碼為:

TextBox1.DataBindings.Add("Text", company, "Employees.Name");

Employess.Name即為用點分隔的名稱層次結構。在這里,Employees為一個集合,將Employees.Name綁定到TextBox會出現什么情況呢?測試后可知,TextBox將顯示Employees集合中第一個Employee的Name屬性。

示例:

界面

代碼實現:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace DataBindingsDemo
12 {
13     public partial class FrmDataBindings : Form
14     {
15         public FrmDataBindings()
16         {
17             InitializeComponent();
18         }
19 
20         private void FrmDataBindings_Load(object sender, EventArgs e)
21         {
22             //綁定到DataTable
23             DataTable dtSource = GetDataTable();
24             this.textBox1.DataBindings.Add("Text", dtSource, "StudentNo");
25             this.textBox2.DataBindings.Add("Text", dtSource, "StudentName");
26             this.textBox3.DataBindings.Add("Text", dtSource, "Sex");
27 
28             //綁定到實體對象
29             Student stu = new Student() { StudentNo=2,StudentName="測試2",Sex=""};
30             //必須是綁定到對象的屬性(此例中綁定到StudentNo,而不是student),
31             this.textBox4.DataBindings.Add("Text", stu, "StudentNo");
32             this.textBox5.DataBindings.Add("Text", stu, "StudentName");
33             this.textBox6.DataBindings.Add("Text", stu, "Sex");
34         }
35 
36         private DataTable GetDataTable()
37         {
38             DataTable dt = new DataTable();
39             DataColumn dcNo = new DataColumn("StudentNo", typeof(Int32));
40             DataColumn dcName = new DataColumn("StudentName", typeof(string));
41             DataColumn dcSex = new DataColumn("Sex", typeof(string));
42             dt.Columns.Add(dcNo);
43             dt.Columns.Add(dcName);
44             dt.Columns.Add(dcSex);
45             dt.Rows.Add(new object[] { 1,"測試",""});
46             return dt;
47         }
48     }
49 
50     public class Student
51     {
52         private int studentNo;
53 
54         public int StudentNo
55         {
56             get { return studentNo; }
57             set { studentNo = value; }
58         }
59 
60         private string studentName;
61 
62         public string StudentName
63         {
64             get { return studentName; }
65             set { studentName = value; }
66         }
67 
68         private string sex;
69 
70         public string Sex
71         {
72             get { return sex; }
73             set { sex = value; }
74         }
75     }
76 }

運行效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM