One of the most powerful aspects of .NET and Windows Forms is data binding. Historically, data binding was used to bind views to data stored in databases. Some database management systems, such as Microsoft Access, have provided GUI APIs to help developers quickly bind to data.
This sample is taken from Chapter 6: "Windows Forms Data Binding" of the Blue Vision Title "Developing .NET Custom Controls and Designers using C#"
Overview
One of the most powerful aspects of .NET and Windows Forms is data binding.
Historically, data binding was used to bind views to data stored
in databases. Some database management systems, such as Microsoft Access,
have provided GUI APIs to help developers quickly bind to data. Each DBMS
normally had its on associated API for data binding purposes. Some even had
no associated API, which forced developers to provide the implementation from
scratch. Binding to other types of data structures, such as arrays, were out
of the question. .NET, however, solves all these problems and more. With .NET,
a client can bind to almost any data structure, including arrays, collections,
data rows, and data views.
Data Binding Concepts
For .NET data binding to be possible,
there must be providers of data and consumers of data. A provider of data
is an object or component that exposes its data to the outside. A consumer
is an object or component that uses the data exposes
by a provider with the intention to display or modify that data. With .NET
data binding, the minimum requirement to support list-based data binding is
for the provider to implement the IList interface.
The IList interface represents an index-based collection.
Data Providers
The following objects implement the IList interface; so they are, inherently, data providers.
Arrays
An array is simply a collection
of objects that can be accessed by a numeric index. Arrays can be
either single-dimensional or multi-dimensional.
DataSet
The DataSet is a .NET representation of a database. It does not, however, need
to actually be connected to a real database. As a matter of fact, it acts as
a "disconnected" data source, with the ability to track changes and
merge new data. When binding to a DataSet, the consumer is responsible for asking
for the particular DataTable to bind to. In some cases, the consumer would really
be binding to the DataSet's default DataViewManager.
DataTable
A DataTable typically represents a table in a database. Though, it may also
be used to represent the structure of an XML element, or the parent of a collection.
A DataTable contains a DataColumn collection and a DataRow collection. Complex
controls, such as the .NET DataGrid, can be bound to a DataTable with ease.
Note that when you bind to a DataTable, you really bind to the table's default
DataView.
DataView
A DataView is simply a customized view of the data in a DataTable. For instance, it may contain all rows sorted by a particular column,
or all rows that match a certain expression. When data binding to a DataView, all controls involved in the binding process will
receive a snapshot of the data at that particular moment. Whenever the underlying
data changes, the bound controls must have some way of knowing how to refresh
themselves. This process will be discussed shortly.
DataViewManager
The DataViewManager represents the entire DataSet. Like the DataView, it is a customized snapshot
view of the DataSet. The only difference is that it also includes relations.
DataColumn
A DataColumn typically represents, and is analogous to, a column in a database table.
Although, it may also represent an XML attribute or an
attributeless XML element. You can only simple-bind
to a DataColumn. This means that only simple controls,
such as a text box, can be bound to a DataColumn.
Other .NET Objects
In actuality, any .NET object may support data binding, but you may not automatically
reap all of the benefits provided by the .NET architecture. Also, when binding
to these objects, only the public properties (not public fields) can be bound
to. Therefore, you must be careful when data binding to data sources exposed
by web services. The public properties of any types returned by a web service
will be converted to public fields in the web service's client proxy code.
Be careful. You can only bind to
the public properties, not the public fields, of data source objects.
Data Consumers
A consumer is an object or
component that uses the data exposed by a provider whose intent is to display
or modify that data. In Windows Forms, a consumer is typically a data-bound
control. Simple data-bound controls include, but are not limited to, text
boxes, labels, check boxes, and radio buttons. These controls can only display
one data value provided by a data source. On the other hand, controls such
as data grids, list boxes and combo boxes can display a list of values. These
controls are therefore referred to as complex data-bound controls.
Comments
Be the first to write a comment
You must me logged in to write a comment.