现在的位置: 首页 > 综合 > 正文

WinForm 2.0 应用一 无代码实现详细信息绑定

2012年09月03日 ⁄ 综合 ⁄ 共 8246字 ⁄ 字号 评论关闭

Your First Data-Bound Windows Forms 2.0 Application

To get your hands dirty early with Windows Forms 2.0 and Visual Studio 2005, the following procedures give a quick demonstration of their easy and powerful new capabilities. You can either follow along if you have a machine with Visual Studio 2005 handy or just use the figures to visualize the process. You will:

1.
Create a Windows application project

2.
Add a new data source and a data connection

3.
Select data objects

4.
Customize data sources control mappings

5.
Generate data-bound controls

6.
Run an application

Don't worry about understanding all of the steps at this point; they will be described in detail in later chapters.

Creating a Windows Application Project

1.
Start Visual Studio 2005. You'll see the Start Page (shown in Figure 1.2). Click Create to display the New Project dialog. If you don't see the Start Page, select File > New > Project from the menu.

Figure 1.2. Visual Studio Start Page

 

 

2.
In the New Project dialog (see Figure 1.3), expand the Visual C# option under Project types, and select the Windows Application template.

Figure 1.3. New Project Dialog

 

 

3.
Name the project FirstDataApp, select a working directory for the application in the Location field, and click OK.

At this point, you will have an open Windows Forms project with the default Form1 class displayed in the designer (see Figure 1.4).

Figure 1.4. Empty Windows Forms Project

 

 

Adding a New Data Source and a Data Connection

1.
From the Data menu, select Add New Data Source. This displays the Data Source Configuration wizard (see Figure 1.5).

Figure 1.5. Source Selection in the Data Source Configuration Wizard

 

 

2.
Select Database as the data source type and click Next. This displays a page to set your data connection (see Figure 1.6).

Figure 1.6. Data Source Configuration Wizard Connection Selection

 

 

Select the data connection that you want to use. The options available depend on whether you have previously configured data connections in Visual Studio 2005. Assuming that you haven't configured a connection to the Northwind database yet, you will need to add a new connection. If you have already set up data connections in Visual Studio 2005, continue with step 7.

3.
Click the New Connection button. The first time you do this in Visual Studio 2005, the dialog shown in Figure 1.7 is displayed so you can select a data source provider.

Figure 1.7. Data Source Provider Selection

 

4.
Under Data source, select Microsoft SQL Server. The option under Data provider defaults to .NET Framework Provider for SQL Server. Click OK.

5.
In the Add Connection dialog (see Figure 1.8), enter localhost as the server name (if you are working with a local default instance of SQL Server 2000 or 2005 with Northwind on it). If you need help getting a database set up, are working with a nondefault instance, or need to work with SQL Server 2005 Express instead, see the book's Web site for instructions.

Figure 1.8. Add Connection Dialog

 

6.
Select Use Windows Authentication, enter Northwind as the database, and click OK.

7.
This redisplays the connection selection step of the Data Source Configuration wizard (Figure 1.6). Click Next. The page in Figure 1.9 is displayed.

Figure 1.9. Saving the Connection String

 

 

8.
To save the connection string information to the application configuration file, accept the default and click Next.

Selecting Data Objects

The last page of the Data Source Configuration wizard (Figure 1.10) displays a tree of the database objects contained in the database (in this case, the Northwind database) you selected in the connection step. This includes tables, views, stored procedures, and functions for a SQL Server database.

Figure 1.10. Database Object Selection

 

 

1.
Expand the Tables node and select the Employees table.

2.
Leave the data set name at the bottom as NorthwindDataSet and click Finish.

By completing this simple wizard, the designer generates approximately 2,000 lines of well-tuned data access code and data type definitions for you.

Customizing Data Sources Control Mappings

This procedure customizes the data source control mappings in Visual Studio.

1.
Open the Data Sources window by selecting Data > Show Data Sources.

2.
Expand the tree of data sources at the Employees level. This shows the tree of controls that can be generated automatically for data binding.

3.
Click on the drop-down arrow next to Photo and select PictureBox as the kind of bound control to be generated (see Figure 1.11).

Figure 1.11. Changing the Bound Control Type for the Photo Field

 

4.
Click on the drop-down arrow next to the Employees table at the top of the tree, changing its bound control type to Details (see Figure 1.12).

Figure 1.12. Changing the Bound Control Type for the Employees Table

 

Generating Data-Bound Controls

Now you are ready to generate some data-bound controls on the form.

1.
Left-click and hold on the Employees node in the Data Sources tree, drag it onto the upper left part of the form, about a half-inch down from the title bar, and release the mouse button.

- The Visual Studio designer generates an entire form with appropriate bound controls for each column in the Employees table. It also generates a Label control next to each data-bound control based on the name of the column, and even does intelligent things with the label such as breaking up the EmployeeID column name into a label whose Text property is set to Employee ID:. The designer also names each of the controls, so instead of getting controls named TextBox1, TextBox2, and so on like you do when you drag controls out from the Toolbox, the designer identifies them with names like employeeIDTextBox and lastNameTextBox.

- An instance of the NorthwindDataSet class is added to the form as a member, along with a table adapter, which is an instance of the EmployeesTableAdapter that was generated in the section Selecting Data Objects. The table adapter fills the data set and persists changes from it back into the database.

- A BindingSource component, which is used to tie the data source to the bound controls, is added.

- A BindingNavigator is also added. This provides navigation controls at the top of the form. These let you page through the records in the table one at a time, add new records, save changes to the records, or delete records.

- All the code is written behind the scenes to hook up the data-binding properties of the components generated, as well as code to retrieve the contents of the Employees table from the database into the data set instance used by the form, and to save changes back to the database after it has been edited through the bound controls.

After doing this simple drag-and-drop operation, the designer writes about 600 lines of code (for you!) to lay out all those controls and hook them up to the data source.

2.
Let's do one last thing in the designer to make the result a little prettier. Select the PictureBox control that was added for the Photo field, to the right of the Photo: label in the form.

3.
In the Properties window in the bottom right of the Visual Studio IDE, scroll through the list of properties and right-click on SizeMode.

4.
Select Zoom in the drop-down list (see Figure 1.13).

Figure 1.13. Setting the SizeMode Property of the PictureBox Control

 

 

Running the Application

1.
To run the application, press F5 or select Debug > Start Debugging from the menu.

You should see the application running, as shown in Figure 1.14. Without writing a single line of code by hand, you have a reasonably complex data access form which lets you view a set of records that has data types including text, dates, and images. You can use the navigation controls at the top of the form to page through the records, edit records, create new records, delete records, and save the changes made to records from memory back to the database.

Figure 1.14. Running FirstDataApp

 

If you aren't saying "Wow!", then you are either really hard to impress or you haven't spent much time trying to write applications like this before using Visual Studio 2005. With past technologies it took writing a lot of codeon par with the thousands of lines of code the designer just wrote for youto get something like this form hooked up and working correctly. And you had to write that same code over and over for every form like this you needed. It is likely that you made some mistakes along the way and had bugs in your forms that were (hopefully) caught in testing. This probably led to digging around in that code to find the problem and get it working correctly. With the designer writing the code, not only do you get it done in a miniscule fraction of the time, but the code is much more likely to be correct the first time for basic scenarios.

At this point you may be wondering, "If it's this simple, why do I need this big book to figure out data binding in Windows Forms 2.0?" Well, the real world is never simple. Although the designer simplifies coding common scenarios like the form we just generated, there will always be complex scenarios that your users or your marketing folks will want your application to support and that the designer isn't capable of coding for you. To address those scenarios, and to better understand all the simpler scenarios where the designer does help you, you need to dig a little deeper and spend a little more time. Also, anytime code is generated for you, it's risky to proceed with development if you don't understand the code that was generated. You will have to maintain that code, and you may have to make direct modifications to it or write similar code by hand to support more advanced scenarios. For all these reasons, you need to understand the material covered in the rest of this book.

抱歉!评论已关闭.