Data binding (translation)

Source: Internet
Author: User
Tags bind
Data Chinese information:

Introduction to data binding and controls

When Microsoft develops form controls and Web page controls, it desperately implements data binding. At that time was still using VB, they have a large length of the functional program editing.

The most efficient data binding is not the dataset we say, but the object, structure, and set of objects. Data binding has a notable feature: A control that implements data binding in a Web page, and we don't do anything else about it. Data binding only read operations, no other operations, that is, the control of the data source to take the information, just use, can not operate. While we are building other objects or UI, we do not need to care about it, and its operation is at a glance.

Windows Forms Control data binding can be read and written, and some operations are more complex. As a result, the data is pulled out of the database and displayed in our UI controls, and once the UI control's data changes, the data in the database changes with it, and of course these operations don't need to be done, but the data changes will inevitably allow us to write more code. Because of the characteristics above, we can use these characteristics to our enterprise and class set, these features include:

1. has modifications to data such as the UI.

2. Allows the DataGrid data to be properly bound.

3. The Neutron object in the DataGrid can be edited.

4. Child objects can be dynamically added or deleted in the DataGrid.

For a simple object, when we modify the correct data, I can use the event to complete the binding of the form data.

With these events, we can make the UI data updated at any time with our modifications.

Once the correct rules in the UI are added to the corrupted data, we must know how to modify it. The same use of proper events to bind the data makes the binding error.

In addition, here we have a lot of optional features to use. For example, a DataGrid is a commonly used bound control. By using our set correctly, we can make the DataGrid control more self-contained.

We can use IBindingList and DataGrid together, which is more convenient.

Finally, there are some notable features in the set use, which we call as child objects. Child objects can use IEditableObject to make the DataGrid more convenient for data editing. When there is invalid data, the child object can use the Idataerrinfo, so that the DataGrid to the wrong place with a draw line.

In short, we just need to write a little bit of code to make the advantages of the DataGrid to play incisively and vividly.

Example of a window binding:

Property bindings for controls on a form are not complex, for example, look at the following simple order class:
Public Class Order
Private MID as String = ""
Private Mcustomer as String = ""
Public Property ID () as String
Get
Return MID
End Get
Set (ByVal Value as String)
MID = Value
End Set
End Property
Public Property Customer () as String
Get
Return Mcustomer
End Get
Set (ByVal Value as String)
Mcustomer = Value
End Set
End Property
End Class
The more specific code here is:

Private MID as String = ""
Private Mcustomer as String = ""
Note that the initialization of the variable here is not a value, in visual Basic. NET is automatically initialized when they declare it, so they are not very special.

Data binding fails because we do not do so. This indicates that the automatic initialization of a variable does not occur when the data is bound to an object, and the uninitialized variable throws an exception when the data is bound.

So the declaration of a variable must precede the data binding, which means that we initialize the variable correctly before the data is bound, so that I avoid the exception

We build a form that binds the data to its control's properties when it is load.


The binding code is as follows:

Private Morder as Order
Private Sub orderentry_load (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles MyBase.Load
Morder = New Order ()
TXTID.DATABINDINGS.ADD ("Text", Morder, "ID")
TXTCUSTOMER.DATABINDINGS.ADD ("Text", Morder, "Customer")
End Sub
In fact, each control on a form has a data-bound connection. This connection library contains a series of data-bound properties and

The properties of the data source. What's more interesting is that we bind several different controls from one data source, as well as I can bind multiple data sources to different control properties.

With simple data-binding code, we can create a lot of complex support interfaces. For example: With the above example code, we put a save button enabled property is bound to the IsValid property of a business object. This button control is valid for the user when the object is ready to be saved.

Remember that this data binding is not straightforward, not only is the data displayed from the object to the window, but the user's changes to the data are automatically returned. These occur when the user browses through each area, for example: When the control loses focus, our user changes the value of the txtID control, This data is updated to the object, which is implemented by the Set property. We do not need to do other data binding operations, the system through its properties to automatically obtain data.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.