In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use the DataBindings property of the TextBox control in the Winform project". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The DataBindings property is a property found in many controls and has two functions. On the one hand, it is used to bind with the data of the database and display the data. On the other hand, it is used for data binding to objects of a control or class. The main focus here is on the latter. The main use is to associate an attribute of an object with a specified attribute of a specified object.
Label, TextBox, and so on all contain the DataBindings attribute, which is of type ControlBindingsCollection and is a collection of Binding classes. The Binding class represents a simple binding between an object property value and a control property value. For example, you can bind the value of the Text property of TextBox to the value of the Text property of Label, so that when the text in TextBox is modified, the text of Label will be modified in time, as shown in the following code:
Label1.DataBindings.Add ("Text", TextBox1, "Text")
In addition to binding the properties of an object to the properties of a control, the Binding class can also bind the property values of the current object in the object list to the properties of the control.
When you create an instance using the constructor of Binding, you must specify three things:
The name of the control property to bind to
Data source
A navigation path in a data source that is resolved to a list or property
Where the data source can be:
Any class that implements IBindingList or ITypedList. Include: DataSet, DataTable, DataView, or DataViewManager.
Implement any index collection class of IList. (the collection must be created and populated before creating the Binding, and all objects in the list must be of the same type, otherwise an exception will be thrown.)
The strongly typed IList of a strongly typed object.
The navigation path can be an empty string (the data source's ToString () method is called by default), a single property name, or a dot-delimited name hierarchy.
What does the name hierarchy mean? For example, we have a Company class that contains the Name property and the Employees property (a collection of all the Employee of the company), while the Employee class contains the Name property. So, if you want to bind the Name property of Company to the Text property of the TextBox control, the code is:
TextBox1.DataBindings.Add ("Text", company, "Name")
If you want to bind the Name property of Employees, the code is:
TextBox1.DataBindings.Add ("Text", company, "Employees.Name")
Employess.Name is a name hierarchy separated by dots. In this case, Employees is a collection, so what happens if you bind Employees.Name to TextBox? After testing, you know that TextBox will display the Name property of the first Employee in the Employees collection.
Example:
Interface
Code implementation:
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) {/ / bind to 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") / bind to entity object Student stu = new Student () {StudentNo=2,StudentName= "Test 2", Sex= "female"}; / / must be bound to the property of the object (in this case, bound to StudentNo, not 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, "test", "male"}); 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;}
Running effect:
This is the end of the content of "how to use the DataBindings property of the TextBox control in the Winform project". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.