WinForm 2.0 应用一 无代码实现详细信息绑定
来源:互联网 发布:java基础简答题 编辑:程序博客网 时间:2024/06/18 06:15
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:
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
Figure 1.2. Visual Studio Start Page
![](mk:@MSITStore:D:/Documents%20and%20Settings/Administrator/桌面/Addison.Wesley.Data.Binding.with.Windows.Forms.2.0.Programming.Smart.Client.Data.Applications.with.dot.NET.Jan.2006.chm::/032126892X/images/01fig02.jpg)
Figure 1.3. New Project Dialog
![](mk:@MSITStore:D:/Documents%20and%20Settings/Administrator/桌面/Addison.Wesley.Data.Binding.with.Windows.Forms.2.0.Programming.Smart.Client.Data.Applications.with.dot.NET.Jan.2006.chm::/032126892X/images/01fig03.jpg)
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
![](mk:@MSITStore:D:/Documents%20and%20Settings/Administrator/桌面/Addison.Wesley.Data.Binding.with.Windows.Forms.2.0.Programming.Smart.Client.Data.Applications.with.dot.NET.Jan.2006.chm::/032126892X/images/01fig04.jpg)
Adding a New Data Source and a Data Connection
Figure 1.5. Source Selection in the Data Source Configuration Wizard
![](mk:@MSITStore:D:/Documents%20and%20Settings/Administrator/桌面/Addison.Wesley.Data.Binding.with.Windows.Forms.2.0.Programming.Smart.Client.Data.Applications.with.dot.NET.Jan.2006.chm::/032126892X/images/01fig05.jpg)
Figure 1.6. Data Source Configuration Wizard Connection Selection
![](mk:@MSITStore:D:/Documents%20and%20Settings/Administrator/桌面/Addison.Wesley.Data.Binding.with.Windows.Forms.2.0.Programming.Smart.Client.Data.Applications.with.dot.NET.Jan.2006.chm::/032126892X/images/01fig06.jpg)
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.
Figure 1.7. Data Source Provider Selection
Figure 1.8. Add Connection Dialog
Figure 1.9. Saving the Connection String
![](mk:@MSITStore:D:/Documents%20and%20Settings/Administrator/桌面/Addison.Wesley.Data.Binding.with.Windows.Forms.2.0.Programming.Smart.Client.Data.Applications.with.dot.NET.Jan.2006.chm::/032126892X/images/01fig09.jpg)
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
![](mk:@MSITStore:D:/Documents%20and%20Settings/Administrator/桌面/Addison.Wesley.Data.Binding.with.Windows.Forms.2.0.Programming.Smart.Client.Data.Applications.with.dot.NET.Jan.2006.chm::/032126892X/images/01fig10.jpg)
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.
Figure 1.11. Changing the Bound Control Type for the Photo Field
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.
-
- 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.
Figure 1.13. Setting the SizeMode Property of the PictureBox Control
![](mk:@MSITStore:D:/Documents%20and%20Settings/Administrator/桌面/Addison.Wesley.Data.Binding.with.Windows.Forms.2.0.Programming.Smart.Client.Data.Applications.with.dot.NET.Jan.2006.chm::/032126892X/images/01fig13.jpg)
Running the Application
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.
- WinForm 2.0 应用一 无代码实现详细信息绑定
- WinForm 2.0 无代码DataGridView绑定
- 图书详细信息接口调用代码实现
- C# WinForm ListBox 绑定数据库代码
- 一、WinForm中TreeView数据绑定
- Js实现上下左右无缝隙滚动代码 一
- Winform 下无闪烁走马灯效果实现
- Winform 下无闪烁走马灯效果实现
- winForm实现拖动无边框窗体
- winform 无边框实现拖拽
- WinForm 无边框窗口 拖动实现
- 实现网站应用钉钉扫码登录,及查询用户详细信息
- c#实现winform关机代码
- 软件的分享、卸载、详细信息、运行的代码实现
- WinForm 2.0 有代码两个DataGridView实现Master/Details
- WinForm TreeView的应用 递归绑定区域树
- WinForm之TreeView的应用—绑定区域树
- [WinForm每日一帖] UltraGrid大数据量的绑定
- nutch-0.8在eclipse中运行
- WinForm 2.0 textBox作为参数的查询,显示在DataGridView
- asp.net中正则表达式常用说明
- WinForm 2.0 下拉框的绑定
- WinForm 2.0 无代码DataGridView绑定
- WinForm 2.0 应用一 无代码实现详细信息绑定
- Iterator源码剖析
- 正则表达式-ASP.NET
- 理解性能测试(一)
- PSP开发英雄在索尼恐吓下黯然离去
- .net 函数大全
- java面试笔试题整理
- IRSHELL 3.8/3.81安装使用教程
- 留住最后的激情