软件编程
位置:首页>> 软件编程>> C#编程>> Winform项目中TextBox控件DataBindings属性

Winform项目中TextBox控件DataBindings属性

作者:.NET开发菜鸟  发布时间:2023-03-29 15:25:15 

标签:Winform,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属性。

示例:

界面

Winform项目中TextBox控件DataBindings属性

代码实现:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataBindingsDemo
{
   public partial class FrmDataBindings : Form
   {
       public FrmDataBindings()
       {
           InitializeComponent();
       }

private void FrmDataBindings_Load(object sender, EventArgs e)
       {
           //绑定到DataTable
           DataTable dtSource = GetDataTable();
           this.textBox1.DataBindings.Add("Text", dtSource, "StudentNo");
           this.textBox2.DataBindings.Add("Text", dtSource, "StudentName");
           this.textBox3.DataBindings.Add("Text", dtSource, "Sex");

//绑定到实体对象
           Student stu = new Student() { StudentNo=2,StudentName="测试2",Sex="女"};
           //必须是绑定到对象的属性(此例中绑定到StudentNo,而不是student),
           this.textBox4.DataBindings.Add("Text", stu, "StudentNo");
           this.textBox5.DataBindings.Add("Text", stu, "StudentName");
           this.textBox6.DataBindings.Add("Text", stu, "Sex");
       }

private DataTable GetDataTable()
       {
           DataTable dt = new DataTable();
           DataColumn dcNo = new DataColumn("StudentNo", typeof(Int32));
           DataColumn dcName = new DataColumn("StudentName", typeof(string));
           DataColumn dcSex = new DataColumn("Sex", typeof(string));
           dt.Columns.Add(dcNo);
           dt.Columns.Add(dcName);
           dt.Columns.Add(dcSex);
           dt.Rows.Add(new object[] { 1,"测试","男"});
           return dt;
       }
   }

public class Student
   {
       private int studentNo;

public int StudentNo
       {
           get { return studentNo; }
           set { studentNo = value; }
       }

private string studentName;

public string StudentName
       {
           get { return studentName; }
           set { studentName = value; }
       }

private string sex;

public string Sex
       {
           get { return sex; }
           set { sex = value; }
       }
   }
}

运行效果:

Winform项目中TextBox控件DataBindings属性

来源:https://www.cnblogs.com/dotnet261010/p/6731126.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com