Binding Data with ADO.NET - Pdf 63

Chapter 21
Binding Data with ADO.NET
After completing this chapter, you will be able to:

Bind data to controls in Windows Forms, WPF, and ASP.NET applications

Understand the classes and technologies that make binding possible

Create working database programs with simple drag-and-drop techniques
Data binding involves connecting display and data-entry controls with a data source in a
way that somewhat automates the data management process. Rather than writing code that
manually transfers data between, say, a DataTable instance and individual TextBox controls,
you configure the controls to understand how they should obtain data from the DataTable.
This simulated simplicity—the true complexity is hidden away inside of .NET controls and
classes—brings Rapid Application Development (RAD) features from pre-.NET versions of
Visual Basic into the .NET world.
This chapter provides brief demonstrations of using data binding in three types of .NET
applications: Windows Forms, Windows Presentation Foundation (WPF), and ASP.NET. In
each case, you’ll create a sample application with simple drag-and-drop techniques, with no
additional code added to enable data migration. A discussion of the technologies involved
follows each example.
Binding Data in Windows Forms
The Windows Forms system supports two types of binding scenarios based on the features
of individual controls: simple and complex. In simple data binding, a control hosts a single
value from a single data record, such as a TextBox that supports displaying or editing a cus-
tomer name. In this arrangement, a form typically hosts multiple controls, each of which is
bound to a specific data row field.
In complex data binding, a single control expresses a list of records, perhaps all records in
a table. The control can include features that indicate how to display the complexities of a
data row in a single control entry. The ListBox control is a typical candidate for complex data
binding.

ect displays a single blank form.
2. Add a data source for the table to be edited. The “Connecting to External Data” section
in Chapter 1, “Introducing ADO.NET 4,” includes step-by-step instructions for creat-
ing such a data source. Follow those instructions. When you reach the Choose Your
Dwonloaded from: iDATA.ws
Chapter 21 Binding Data with ADO.NET
349
Database Objects panel in step 14 of the instructions, select the CourseCatalog table
from the list. Type CourseDataSet in the DataSet Name field. Click Finish to add the
DataSet to the project.
3. The Data Sources panel in Visual Studio now contains a CourseDataSet tree with the
CourseCatalog table as a branch within it. Expand the CourseCatalog branch to see the
individual fields associated with the table: four text fields and two Boolean fields.
Dwonloaded from: iDATA.ws
350
Microsoft ADO.NET 4 Step by Step
4. The CourseCatalog branch is actually a drop-down list; click its name to enable the list.
Expand the list and ensure that DataGridView is selected.
5. Drag the CourseCatalog name from the Data Sources panel and drop it on the blank
form’s surface. This action adds a DataGridView control to the form with columns for
each of the table’s fields. It also adds a toolbar-style control and several non-visual con-
trols to the bottom of the design window. Resize the form and grid control as needed.
6. Run the program. When the form appears, the grid will display any records already
found in the CourseCatalog table. Note that the data is fully editable; you can add, up-
date, and delete records in the table through the form.
Dwonloaded from: iDATA.ws
Chapter 21 Binding Data with ADO.NET
351
Creating Simple-Bound Applications
The DataGridView control and the accompanying VCR-style control in the complex-bound

ficient code to enable source-to-user data management.
When you drag a table to the form’s surface in either the complex-style or simple-style bind-
ings, Visual Studio builds a hierarchy of classes that work together to transport data between
the database and the user. The following list identifies the members of this chain, moving
from the source-side to the form-side of the interaction.

TableAdapter As discussed in Chapter 11, a data adapter enables bidirectional move-
ment of a table’s content. Because the bound controls display data and accept updates
from the user, the adapter keeps the database and the form in sync.

TableAdapterManager This object, added to the form by Visual Studio, assists the
TableAdapter in hierarchical-data environments. The CourseCatalog examples shown
above do not include hierarchical data.
Dwonloaded from: iDATA.ws
Chapter 21 Binding Data with ADO.NET
353

DataSet This is the wizard-built DataSet instance added to the Data Sources panel.
It’s actually a strongly typed data set, which is not discussed in this book other than to
point out how the Entity Framework (EF) expands on the basic strongly typed data set
functionality.

BindingSource Although Windows Forms controls can bind directly to ADO.NET
DataTable instances, such a relationship provides only minimal functionality. The
BindingSource class enriches this relationship by adding change notifications, error
management, and consistent movement functionality between the records of a source
data table.

BindingContext The binding context doesn’t appear as one of the controls added by
Visual Studio, but it is present. It appears as a member of the form itself and exists in

Me.Validate()
Me.CourseCatalogBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CourseDataSet)
This code finishes up all pending edits and saves all changes to the database, bringing every-
thing back in sync.
As convenient as these data binding examples are, it is rare that an application can meet us-
ability needs without providing additional custom code. In addition to using these simple
techniques, you can augment the generated controls and code with your own source code or
use the generated source as a basis for designing your own data-bound environments.
Binding Data in WPF
Windows Presentation Foundation (WPF) applications use an XML-based schema language
called XAML to define the user interface and behavioral aspects of an application. Visual
Studio includes support for building WPF/XAML applications, presenting a design experience
that is similar to standard Windows Forms development.
Note
An introduction to the Windows Presentation Foundation and its XML-based way of de-
scribing application elements is beyond the scope of this book.
Creating Data-Bound WPF Applications
WPF applications in Visual Studio support the same type of drag-and-drop data-bound ap-
plication building features present in Windows Forms projects, albeit with some variations.
The following examples guide you through the construction of a data-bound WPF project.
Dwonloaded from: iDATA.ws


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status