Use the property editor to flexibly adjust the binding fields of Data Controls

Source: Internet
Author: User

In visual studio.net, you can easily drag fields from the data source to the form by creating a data source. In this way, you can quickly implement the data access function, microsoft also provides flexible modification operations in the Data Control Attribute Editor so that we can flexibly adjust the binding fields of a control as needed during design, let me recall this process:

1. Create a Data source:

At this time, we can see through the solution browser that the wizard adds a jwinfodataset for the project. XSD, an XML schema file that contains the dataset schema information, in which jwinfodataset. designer. CS is the class file corresponding to dataset, while jwinfodataset. xsc and jwinfodataset. XSS contains the layout information of dataset in the designer:

2. Bind controls and the Visual Studio designer to automatically bind controls in the drag-and-drop mode:

First, we can select the control that is displayed after binding:

By default, datagridview is used to display the data. You can select the details, and select the display control for each table field:

After selection, you only need to drag the corresponding field to the form (you can also drag the entire table to select all the table fields), you can automatically complete the field binding:

The development environment will automatically create corresponding controls to complete data binding. At this time, we observe that there are multiple display controls on the form that correspond to the fields of the dragged data table, these controls are added at the bottom of the Form Designer. That is to say, the dataset, bindingsource, tableadapter, tableadaptermanager, and bindingnavigator controls are automatically generated during the drag operation. We will discuss these controls later:

3. Next we will introduce how to flexibly adjust the binding fields of the Data Control in the property designer:

First, select the control for which we want to adjust the bound field, and press the F4 shortcut key to open the attribute settings window:

Expand databindings. You can see that the text property is a drop-down selection box. After clicking it, we can see that the fields bound to the control can be flexibly adjusted here:

    • You can select fields in the bindingsource generated by the designer.
    • You can select a project data source under another data source.
    • You can select a form data source instance under another data source.

After further expansion, we found that we only dragged the class table when dragging, so we can only select the fields that associate the control to the class table in bindingsource, if we need to bind fields to the student table, we need to select other data sources for implementation:

The project data source is automatically generated by the development tool for the project after we create the data source through the data source creation wizard. It is at the project level and all forms can be shared:

The form1 list instance belongs to the form level, which is automatically generated by the development environment when we drag the data source browser. This is its definition:

Private jwinfodataset;

That is, other forms of the data source cannot be accessed.

Next, let's take a look at these controls:

Jwinfodataset is a dataset control that can be understood by everyone. It is equivalent to a memory database that can protect information such as able and datarelation.

The bindingsource control is used to bind data tables. Let's take a look at the automatically generatedCodeTo get a clear understanding:

Private system. Windows. Forms. bindingsource class bindingsource;

//
// Class bindingsource
//
This. Class bindingsource. datamember = "class ";
This. Class bindingsource. datasource = This. jwinfodataset;

That is to say, bindingsource completes binding by setting the datasource and datamember attributes. For example, set datasource to the jwinfodataset we created and set datamember as the class, in this way, the table is bound to the class table. Then we can see the Code as follows:

This. Class bindingnavigator. bindingsource = This. Class bindingsource;

This is another automatically generated control, bindingnavigator. After bindingsource completes data binding, it is used to publish movefirst, moveprevious, movenext, movelast, and other methods to bingdingnavigator. There are many buttons on the bindingnavigator control, these buttons can browse records and add or delete records. Each button is an instance of toolstrpitem. The bindingnavigator control listens to the click events of these buttons and then calls the associated bindingsource method accordingly, for example, if you click the button corresponding to the movefirstitem attribute of bindingnavigator, bindingnavigator automatically calls the movefirst method of bindingsource.

There is also a tableadapter control, which is similar to dataadapter, which can get data and then fill the dataset. Let's take a look at the automatically generated code:

Private databindingdemo. jwinfodatasettableadapters. Class tableadapter class tableadapter;

This. Class tableadapter = new databindingdemo. jwinfodatasettableadapters. Class tableadapter ();

Note the code here. The automatically generated tableadapter is a strongly typed tableadapter!

//
// Class tableadapter
//
This. Class tableadapter. clearbeforefill = true;

The clearbeforefill attribute allows you to determine whether to clear existing data in the dataset before filling in data.

Finally, tableadapter completes data loading during form loading:

This. Class tableadapter. Fill (this. jwinfodataset. Class );

We can see that there is no setup for tableadapter. We can fill the dataset directly by defining and instantiating it, to understand the secrets, we need to study the data source created by the Data Source Creation wizard. What code is generated for the development environment:

First, we can see that in the jwinfodataset. Designer. CS file, a namespace "dataset name + tableadapters" is defined. Why is tableadapter? Because each different table has a corresponding tableadapter, there will be many tableadapters. Then we can see the tableadapter corresponding to the class table: the definition of the class tableadapter. through several field members of this class: _ adapter, _ connection, _ transaction, and _ commandcollection, we know that, we have seen the simple definition above and then directly fill in the data. The code here is needed for support. It is hard to imagine that these four members have completed database connection and executed SQL commands, then, use the fill method of sqldataadapter to implement and support the above concise code. Then, carefully study the definition of tableadapter in this class:

There is an initadapter method that completes the initialization of tableadapter: sets the connected table, and the correspondence between fields, as well as statements for querying, deleting, and updating commands.

Public Virtual int fill (jwinfodataset. Class datatable ){
This. Adapter. selectcommand = This. commandcollection [0];
If (this. clearbeforefill = true )){
Datatable. Clear ();
}
Int returnvalue = This. Adapter. Fill (datatable );
Return returnvalue;
}

Finally, we can see its fill method. This method runs the SELECT command and fills the able table through sqldataadapter.

After careful analysis, we can see that the fill method of the tableadapter class needs to input a jwinfodataset. A data table of the class datatable type receives data, which is also a strong type. The so-called strong type means that this datatable or tableadapter is no longer. NET provides a common class. It cannot be associated with any table. It can only be associated with a specific type of table, such as a class table. Let's take a look at the definition of the jwinfodataset. Class datatable class:

First, the automatically generated code comment also tells us that this is a strong type. This class is automatically generated after we drag it. According to the field we drag, there should be a corresponding definition here.

Let's take a look at its method definition for adding rows:

The method is named "add class row". Why do you write this? Because it is a strong type, other row cannot be added. It must be the class row and the class row must be numbered by the class, the class name, Department number, and professional code are composed of four columns, and the order cannot be messy!

If you continue to analyze the class row, you can see that this is also a strong type:

This class encapsulates the row information in a class table.

The last control is not analyzed, that is, tableadaptermanager, which can be understood as a class for managing tableadapter, because there are many tableadapters, and after we modify the record, you can complete all updates in one place:

This. tableadaptermanager. updateall (this. jwinfodataset );

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.