Danh mục

Windows Forms Controls and Data Binding

Số trang: 9      Loại file: pdf      Dung lượng: 25.15 KB      Lượt xem: 8      Lượt tải: 0    
Thư viện của tui

Phí lưu trữ: miễn phí Tải xuống file đầy đủ (9 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Windows Forms Điều khiển và dữ liệu tài sản Binding Nhiều nhất Windows Các hình thức kiểm soát có thể được đính kèm, hoặc bị ràng buộc, với một nguồn dữ liệu. Sau khi họ bị ràng buộc, giá trị trong nguồn dữ liệu thay đổi giá trị tài sản bị ràng buộc và ngược lại.
Nội dung trích xuất từ tài liệu:
Windows Forms Controls and Data Binding Windows Forms Controls and Data BindingMany properties of most Windows Forms controls can be attached, or bound, to a datasource. After they are bound, the value in the data source changes the value of the boundproperty and vice versa. You have already seen data binding in action by using theTextBox and DataGridView controls in the DisplayProducts project in Chapter 23. Thecontrols on the form were bound to BindingSource objects belonging to a DataSetcontaining records from the Suppliers and Products table in the database.Windows Forms controls support two types of data binding: simple and complex. Simpledata binding allows you to attach a property of a control or form to a single value in adata source, and complex data binding is specifically used to attach a control to a list ofvalues. Simple data binding is useful for controls such as TextBox or Label that onlydisplay a single value. Complex data binding is more commonly used with controls thatcan display multiple values, such as ListBox, ComboBox, or DataGridView.Defining a DataSet and Using Simple Data BindingYou use simple data binding to display a single value from a data source. A data sourcecan be almost anything, from a cell in a DataSet to the value of a property of anothercontrol to a simple variable. You can perform simple data binding at design time by usingthe Data-Bindings property of a control. In the exercises that follow, you will define anew DataSet that defines a data source returning a single row, and then bind the Textproperty of a Label control to a BindingSource object for this DataSet.Defining a DataSet class 1. In Visual Studio 2005, create a new project called ProductsMaintenance in the \Microsoft Press\Visual CSharp Step by Step\Chapter 24 folder in your My Documents folder by using the Windows Application template. 2. In the Project menu, click Add New Item. The Add New Item dialog box appears, displaying templates for objects that can be added to a project. 3. In the Add New Item dialog box, click the DataSet template, type NorthwindDataSet.xsd for the name, and then click Add. NOTE DataSet definitions should always be created in a file with the extension “.xsd.” DataSet definitions are actually XML schemas, which Visual Studio 2005 uses to generate C# code when the application is built. The DataSet Designer window appears.4. In the Toolbox, expand the DataSet category if necessary, and then click the TableAdapter tool. Click anywhere in the DataSet Designer window. A DataTable and TableAdapter object are added to the DataSet Designer window, and the TableAdapter Configuration Wizard appears.5. In the Choose Your Data Connection page of the TableAdapter Configuration Wizard, click Next. NOTE The Choose Your Data Connection page in the TableAdapter Configuration Wizard will either display NorthwindConnectionString or YourServer\sqlexpress.Northwind.dbo for the data connection, depending on whether you have left Visual Studio 2005 running since performing the exercises in Chapter 23, or have closed it down and restarted it. Either data connection will work for this exercise.6. If the “Save the connection string to the application configuration file” page appears, specify that the connection string should be saved with the name NorthwindConnectionString and click Next. The Choose a Command Type page appears.7. The “Choose a Command Type” prompts you to specify how the TableAdapter should access the database. You can provide your own SQL statements, you can get the Wizard to generate stored procedures that encapsulate SQL statements for you, or you can use pre-existing stored procedures that a database developer has already created. Select Use SQL statements, and then click Next. The Enter a SQL Statement page appears.8. Type the following SQL SELECT statement that calculates the number of rows in the Products table:9. SELECT COUNT(*) AS NumProducts FROM Products10. Click Advanced Options. The Advanced Options dialog box appears. Apart from retrieving data from the database, a TableAdapter object can also insert, update, and delete rows. The Wizard will generate SQL INSERT, UPDATE, and DELETE statements automatically, using the table you specified in the SQL SELECT statement (Products in this example).11. The SQL SELECT statement used to fetch data simply counts the number of rows in the Products table. It does not make sense to generate SQL INSERT, UPDATE, or DELETE statements as this value is not directly updateable. Clear the “Generate Insert, Update, and Delete statements” check box and then click OK.12. In the Table Adapter Configuration Wizard, click Next. The “Choose Methods to Generate” dialog box appears.13. A TableAdapter can generate two methods for populating a DataTable object with the rows retrieved from the database: the Fill method which expects an existing DataTable or DataSet as a parameter which is filled with the data, and the GetData method which creates a new DataTable and fills it. Leave both methods selected, and then click Next.14. The Wizard uses the information you have supplied and generates the new TableAdapter class. Click Finish to close the Wizard. A TableAdapter class called DataTable1TableAdapter, and a corresponding DataTable class called DataTable1, appear in the DataSet Designer window.15. In the DataSet Designer window, click the DataTable1 item. Using the Properties window, change its Name property to NumProductsTable. Click the DataTable1TableAdapter and change its name to NumProductsTableTableAdapter. The objects in the DataSet Designer window should look like the following graphic: Notice that NumProductsTable DataTable contains a ...

Tài liệu được xem nhiều: