Data Binding


Dealing with data is one aspect of your application development that may not require you to write custom code. Data-centric applications are fully supported in Windows Forms, and you can manage data sources completely in VS05's Data Sources window, shown in Figure 1.24.

Figure 1.24. VS05 Data Sources Window


You open the Data Sources window by clicking Data | Show Data Sources (Shift+Alt+D).

From the Data Sources window, you can add a new data source to your project either by clicking the Add New Data Source link label (if your project has no data sources) or from the tool strip (to add other data sources). Either action opens the Data Source Configuration Wizard, shown in Figure 1.25.

Figure 1.25. Data Source Configuration Wizard


The Data Source Configuration Wizard allows us to create and configure a data source for tables in a database, objects located in the current assembly, and objects located in another assembly or web service. Figure 1.26 shows the result of creating a data source for the Northwind database's Employees table.

Figure 1.26. Employees Data Source in VS05


After you have a data source, you can perform a variety of additional configurations, as covered in Chapter 16: Data Binding Basics. The most interesting feature is that you can create a fully operational data-bound form simply by dragging data-bound controls straight onto the form from the Data Sources window. Figure 1.27 shows the result of dragging the Employees data source onto a form.

Figure 1.27. Result of Dragging a Data Source onto a Form


The data source is represented by two data components: a data set, a DataSet-derived class generated by the Designer to hold Employees data for the Employees table; and a table adapter to shuttle data between the data set's Employees table and the Employees table in the Northwind database. The UI is composed of two controls: a DataGridView to display the data, and a BindingNavigator to provide VCR-style navigation of the data.

Between the data components and the UI controls lies the BindingSource component; BindingSource consumes item or collection types and exposes them as data, which can be associated with controls in a special relationship known as data binding. Data binding a control to a data source provides for bidirectional communication between the control and the data source so that when the data is modified in one place, it's propagated to the other. Built on top of this basic concept are many additional levels of data binding support that item or collection types can implement, although it is often difficult and time consuming. However, the BindingSource component's major role is to "upgrade" such types to a satisfactory minimum level of data binding support, which includes currency management, inserts, updates, deletes, and change notification.

Figure 1.28 shows the resulting application executing without a single change to the code generated by the Windows Forms Designer.

Figure 1.28. Full Data Binding Without Code


We've scratched only the surface of what can be done with data binding. For more, read Chapter 16: Data Binding Basics, and Chapter 17: Applied Data Binding. Also, while some types of applications deal with lists of data located in places like databases, other applications deal with document-style data. Such applications have special requirements that are covered in Appendix F: Document Management.




Windows Forms 2.0 Programming
Windows Forms 2.0 Programming (Microsoft .NET Development Series)
ISBN: 0321267966
EAN: 2147483647
Year: 2006
Pages: 216

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net