Windows Forms Data Binding Technology

Source: Internet
Author: User

The bindingsource component is one of the most important innovations in Windows Forms data binding. It can encapsulate data sources for forms, making it easier to bind control data. Generally, a bindingsource component is added to the form, bindingsource component is bound to the data source, and controls on the form are bound to the bindingsource component. The bindingnavigator control is usually used with the bindingsource component to browse the data source of the bindingsource component.

As shown in figure 1 of Data Binding:

 

1. The data binding procedure is as follows:

(1) set the datamember and datasource attributes of the bindingsource component:

Dataset DS = new dataset ();

Oledbdataadapter da = new oledbdataadapter (SQL, Conn );

Da. Fill (DS, "user"); // generates a data source, that is, the dataset component.

Bdsuser. datamember = Ds. Tables [0]. tablename; // The bdsuser is the bindingsource component.

Bdsuser. datasource = Ds;

(2) The Control calls the data binding method:

Public binding add (

String propertyname,

Object datasource,

String datamember

)

Propertyname

The name of the control property to bind.

Datasource

Indicates the object of the data source.

Datamember

The name of the field to bind.

2. Data Binding Methods for common controls

(1) text box Data Binding

Generally, data is bound to the text attribute of the text box. The Code is as follows:

Txtname. databindings. Add ("text", bdsuser, "User Name ");

(2) bind data in the combo box

You can bind the valuemember and displaymember attributes of the combo box respectively:

Cmbpriority. valuemember = "QX ";

Cmbpriority. displaymember = "QX ";

Cmbpriority. datasource = bdsqx;

In addition, you can bind data to the selectedvalue attribute

Cmbpriority. databindings. Add ("selectedvalue", bdsuser, "permission ");

(3) Data Binding of the datagridview

The datagridview control provides powerful and flexible functions to display data in tables. You can bind a data source to the datagridview control by setting the datasource attribute:

Dgvuser. datasource = bdsuser;

The bindingnavigator control is a set of standard buttons used to browse and process form data sources, including the first, last, next, last, and total number of data records. The bindingnavigator control inherits all the features and functions of the toolstrip class. It also plays the role of a container and can contain toolstriplabel, toolstriptextbox, toolstripbutton, and other controls. In the form design phase, select the toolstripitem control to be added to bindingnavigator from the following list box, as shown in Figure 2:

During use, the bindingsource attribute of the bindingnavigator control is usually set to the bindingsource component to be browsed, for example:

Bdnuser. bindingsource = bdsuser;

4. You can use the preceding controls to complete a basic management information system. The program running interface is shown in figure 3:

You can click to download the complete code (Note: no permission table is created in the database to simplify the Code ).

Related Article

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.