Data binding in Windows forms (ii)

Source: Internet
Author: User
Tags bind implement
window| Data Running
We run this program to see if the state can be displayed properly.
1. Press F5 to run the program.
2. Click the countries dropdown box to see if the national data can already be displayed. If normal, you can see the program shown in Figure 8 below:

Figure 8. Using ComboBox to display small datasets can improve performance
Query with parameters to display data
Now you can see the national data in ComboBox, and then we're going to choose a country in the ComboBox that only displays customer information for the country in the DataGrid. We follow these steps to do this:
1. Delete the code that reads the data in the DataGrid in the form's Load event.
2. Modify the SQL query statement for the SelectCommand attribute of SqlDataAdapter1, and add the parameters we want to use in the query statement.
3. Add code to handle events after the user chooses the country.
Delete the code that reads the DataGrid data in the Load event of the form
Because the data in the DataGrid will be determined by the selected country in the ComboBox, we no longer need the code used in the form's Load event. Delete the following code from the form's Load event:
Dscustomers1.clear ()
SqlDataAdapter1.Fill (DsCustomers1, "Customers")
Modify the SelectCommand property
We then modify the SqlDataAdapter1 SelectCommand properties and add the parameters to the query. Follow these steps:
1. In the Form Design window, click the SqlDataAdapter1 object.
2. Press F4 to display the Properties window.
3. Click the + number to the left of the SelectCommand to expand the SelectCommand properties.
4. Click the CommandText property and click Build (...). To display the Query Builder dialog box.
5. Add the following where statement to the SQL query statement:
SELECT CustomerID, CompanyName, ContactName, Country
From Customers
WHERE Country = @CountryParam
6. Click OK
7. Click SqlDataAdapter1 Object
8. Click Data, click Generate DataSet, and then click OK to regenerate the existing dataset.

Figure 9. Adding query parameters to SQL query statements
Add code to handle events after the user chooses the country
When the user chooses the country, we add code to change the data in the DataGrid. This function can be achieved by following these steps:
1. Click the Cbocountry selectedindexchanged event
2. Set the query parameter of the DataGrid to the value selected in Cbocountry
3. Populating data in a dataset
Follow these steps to add code:
1. Open Form as design window
2. Double-click Cbocountry to display the code for the SelectedIndexChanged event. This event occurs when the user changes the Cbocountry value of the selection.
3. In this event handwriting the following code:
Private Sub Cbocountry_selectedindexchanged (_
ByVal sender as System.Object, _
ByVal e As System.EventArgs) _
Handles cbocountry.selectedindexchanged
' Get the Parameter object and Set value
With SqlDataAdapter1.SelectCommand.Parameters
. Item ("@CountryParam"). Value = _
Cbocountry.selectedvalue
End With
' Clear the DataSet
Dscustomers1.clear ()
' Load the dataset using the parameter value
SqlDataAdapter1.Fill (DsCustomers1, "Customers")
End Sub
We use the SqlDataAdapter1 SelectCommand property to get the parameter object and then set the Value property of this object to the Cbocountry SelectedValue property. Because we set the Cbocountry ValueMember property to the Country field, the Cbocountry selectedvalue attribute is the value of the country. After setting the Value property, you can populate the dataset with the data, and the DataGrid automatically displays the customer information for the corresponding country.
Run
Now it's time to see how our program works.
1. Press F5 to run the program.
2. Select a country in the ComboBox to see the corresponding country's client information in the DataGrid
3. Select different countries to display customer information from different countries
D. Data binding using a TextBox
The previous example uses a DataGrid to display data, and in our program it is also important to use a TextBox to display individual row data for editing. This article does not edit the data, we only talk about how to display the data in the textbox. The following are the main steps:
1. Generate a form similar to Figure 10
2. Generate, configure the dataset you want to use
3. Add controls to the form and bind them to the data source
4. Add navigation buttons, provide a line to browse the function of the data
Following the steps above, we create a dataset that reads the Customers table. When adding SqlDataAdapter, we select an existing connection to the Northwind database, generating SQL query statements from the Customers table in CustomerID, CompanyName, ContactName, and ContactTitle columns. You can then add the control and bind the data.

Figure 10. We use the simple example window
Adding controls to form and binding data
Add controls to the form, and set their properties to the values specified in table 1:
Table 1. Controls for form (shown in Figure 10)
Control Type Property Value
Label Name Label1
Text CustomerID
TextBox Name Txtcustomerid
Text Blank
Label Name Label2
Text company Name
TextBox Name Txtcompanyname
Text Blank
Label Name Label3
Text Contact Name
TextBox Name Txtcontactname
Text Blank
Label Name Label4
Text Contact Title
TextBox Name Txtcontacttitle
Text Blank
CommandButton Name Btnprevious
Text <
CommandButton Name Btnnext
Text >
Then you bind each textbox to a column in the dataset, and we follow the steps below:
1. Select a textbox to bind data to
2. Press F4 to display the Properties window
3. Click to expand DataBindings Properties
4. Under the DataBindings property, select the Text property
5. Open the Drop-down list to bind the data to the corresponding column.
For example: To bind Txtcustomerid to the CustomerID column, click on the DsCustomers1 + number, and then select CustomerID.
Once all the textbox is bound, as in the DataGrid, we also want to export the data in the form's Load event by handwriting code. Here is the code we added:
Public Sub New ()
MyBase.New ()
' This are required by the
' Windows Form Designer.
InitializeComponent ()
' Add any initialization
' After the InitializeComponent ()
Dscustomers1.clear ()
SqlDataAdapter1.Fill (DsCustomers1, "Customers")
End Sub
Add a button to implement a line of navigation
The final step is to add buttons and code to allow the user to implement a line of navigation. The button code for the forward navigation looks like this:
Private Sub Btnprevious_click _
(ByVal sender as System.Object, ByVal E As _
System.EventArgs) Handles Btnprevious.click
Me.BindingContext (DsCustomers1, _
"Customers"). Position-= 1
End Sub
Use BindingContext in your program to reduce the data set's record pointers. BindingContext tracks the current item for each data source on the form. The button code for the backward navigation is as follows:
Private Sub Btnnext_click _
(ByVal sender as Object, ByVal e As _
System.EventArgs) Handles Btnnext.click
Me.BindingContext (DsCustomers1, _
"Customers"). Position + 1
End Sub
Use BindingContext in your program to increase the data set's record pointer. Our example results are as shown in Figure 10.
Note: As with the DataGrid control example, in a practical application, we also want to reduce the data displayed on the form. For example, we should add a ComboBox to the form and allow the user to select the customer profile in the specified country. The role of the navigation button is only achieved in this country's customer profile.
Different points in Visual Basic 6.0
Data binding with Windows Forms are far more robust than the data binding in Visual Basic 6.0. With Visual Basic 6.0, your had little control over how the data is bound or what is going on underneath the covers. Using Visual Basic 6.0, when you are used bound forms you added a data control and data entry controls to a form. You are were basically stuck with the functionality this data control provided. It is very difficult to troubleshoot or modify the behavior of the "Data Control", because Microsoft did not reveal the SOU RCE code behind to you.
The Using data binding with Windows Forms and Ado.net, which are have much of the better control and the data are bound and how the form Beh Aves. The Data binding utilizes the Ado.net classes and generates class code that you can view and modify. This means so when things don ' t work, or don t work the way, and you intended them to, and you are don't left with your hands Up in the air as your were with Visual Basic 6.0. The Data binding with the "limitations in Visual Basic 6.0 is not the" optimal choice for production applications; Data binding with Windows Forms and ado.net are a viable option for the. NET Developer.
/*
. NET is much more robust than data binding in Visual Basic 6.0. In Visual Basic 6.0, there are very few data-bound controls that we can use, and the backend programs also don't support enough. In Visual Basic 6.0,
*/
E. Summary
Data binding allows us to save quite a lot of time when writing programs. With data binding, you no longer need to write all of the bound data like in Visual Basic 6.0. In this article, we describe how to use a specific object that connects to a SQL Server database, and the same objects are connected to other databases. In general, none of these need to be written too much code.
In this article, we have learned:
1. Basic knowledge of data binding
2. How to generate a form for data binding
3. How to collaborate with TextBox, ComboBox, DataGrid
4. How to limit the data displayed in the DataGrid
5. How to create a form for data navigation
6. How to achieve data navigation




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.