Ch08_Data Binding to Controls.pdf
(
155 KB
)
Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Chapter 8
Data Binding to Controls
In this chapter, we examine data binding, the ability to associate non-GUI objects that hold
and maintain data with the controls that present and receive that data.
[Comment 8cs.1]
Data binding .................................................................................................................................................. 1
Data Bindable Controls .................................................................................................................... 3
Data Bindable Objects ..................................................................................................................... 4
Simple Data Binding.................................................................................................................................... 14
Using Simple Data Binding with ListBoxes and ComboBoxes ...................................................... 15
Complex Data Binding ................................................................................................................................ 16
The Bindings Collection ................................................................................................................. 17
Formatting and Parsing.................................................................................................................. 18
The DataGrid Control .................................................................................................................................. 21
Using Simple Data Binding with the DataGrid ............................................................................... 24
Styling the Display of Data in a DataGrid.......................................................................................25
Creating Table and Column Styles ................................................................................................27
Creating Styles at Run Time .......................................................................................................... 30
Responding to User Input .............................................................................................................. 32
Using Complex Data Binding with the DataGrid ............................................................................ 35
Accessing DataGrid Information .................................................................................................... 36
Providing a Drill Down Capability................................................................................................... 41
Providing an In-place Edit Capability ............................................................................................. 46
Providing an Automated In-place Edit Capability........................................................................... 51
Conclusion .................................................................................................................................................. 59
Data binding
Most controls, especially single-item and list-based controls, display and accept data. Most
data, when it is not being displayed or modified within a control is kept in a database. And we're
using the term database quite loosely here; loosely enough to include anything from an
ArrayList to a SQL Server database. Since the data is kept in a database, and since we need to
display and modify it through the use of controls, we need a simple, consistent, property-
oriented technique for moving data between our database and our controls. Data binding
provides this to us. It is the ability to tie a control to a source of data, simplifying the movement
of data within a data-centric application. It specifically was developed to facilitate the movement
of data between the business tier and the presentation tier of a traditional three-tiered
application.
[Comment 8cs.2]
Figure 8-1: Application tier
s
[Comment 8cs.3]
We cannot actually go directly from a database to and from a control, but we can go from an
object containing the data to and from a control. The data object might be an ArrayList, such as
our ArrayList of Project task structures used in the previous chapter; or it might be a database
Chapter 8 – Data Binding Page 1
Copyright © 2002-2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
specific object, such as a
System.Data.DataView
object, an object that looks very much like a
relational database table. One benefit of data objects is that they provide the presentation layer
an independence from the database layer. A
DataTable
, for instance, has the same PMEs
regardless of the database product that persists the data. Thus your presentation layer code
remains the same whether your database is SQL Server CE or a home-grown collection of
files.
[Comment 8cs.4]
Data binding is a two way relationship. Any change in the data object data will be reflected
in the control and vice versa. This eliminates the need to reload the control whenever the data
in the data object changes. It also means that any bindable control can be used to update the
data object. For even controls that do not accept user input of data, such as the
Label
,
DataGrid
, and
ListBox
/
ComboBox
, provide for programmatic update of their properties, as in
myLabel.Text = “ABC”
. Data binding ensures that this change occurs in the bound data
object as it occurs in the control.
[Comment 8cs.5]
All visible controls are bindable; but some are a better suited to it than others. Controls that
are typically used with data binding include
Label
,
TextBox
,
ComboBox
,
ListBox
, and the
DataGrid
control. The
DataGrid
control, which will be introduced later in this chapter, is the
only control written entirely in Managed Code, and was specifically developed for use with data
binding. Figure 8-2 shows the Project data that was introduced in the previous chapter being
displayed in a
DataGrid
, without any styling applied.
[Comment 8cs.6]
Figure 8-2: Project Data Displayed in a DataGrid Control
[Comment 8cs.7]
Since the subject of this chapter is data binding, we are only interested here in the
movement of data between the data objects and the controls. The subject of moving data
between the data objects and the underlying databases is covered in Part III - Data
Management.
[Comment 8cs.8]
Chapter 8 – Data Binding Page 2
Copyright © 2002-2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Data Bindable Controls
Controls differ in their data binding capabilities and in the method that is used to do the data
binding. Some controls can display more data than others, some controls accept user input
while others are display only, and some can take advantage of
simple data binding
while others
require
complex data binding
.
[Comment 8cs.9]
Consider the
System.Data.DataTable
, shown in Figure 8-3, which holds the Project data
mentioned above. A
DataGrid
can display the entire table, while a
ListBox
or
ComboBox
is
slightly less capable, being able to display only a single column from the table, and single-
valued controls such as
TextBoxes
and
Labels
are able to display but a single data element.
[Comment 8cs.10]
Figure 8-3: A DataTable Containing Project Informatio
n
[Comment 8cs.11]
Figure 8-4 shows a
ComboBox
displaying one column of information from a bound
DataTable
of Project data, while the
TextBoxes
display data elements of the currently selected
project.
[Comment 8cs.12]
Figure 8-4: Different Controls Bound to the Same Data
[Comment 8cs.13]
Controls that are capable of displaying an entire table, or at least a column of that table, use
a mechanism called
simple data binding
, which involves the control’s
DataSource
property.
Controls that only display a single data element require
complex data binding
, which is done
through the
BindingsCollection
property of the control. All objects that inherit from the base
class of
Control
have a
BindingsCollection
, but only objects that can display a list or a
table, such as a
DataGrid
,
ListBox
, or
ComboBox
, will have a
DataSource
property.
[Comment
8cs.14]
Not only are there inevitable limitations to the data binding capability of each control class,
there are also limitations on the data objects that can be bound to those controls in the Compact
Framework.
[Comment 8cs.15]
Chapter 8 – Data Binding Page 3
Copyright © 2002-2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Data Bindable Objects
As was mentioned earlier, data objects hold data but they have no ability to display that
data, the control must do the display of the data. .NET Compact Framework limits bindable
data objects to those data objects that have a
tabular
data structure. Having a tabular data
structure means that every item in the data object must have the same structure as every other
item in that data object. An
ArrayList
of
Project
structures qualifies as tabular because
every item in it is an item of
Project
structure data type. The same would true for the
ArrayList
of
Tasks
introduced in the previous chapter, for every item is an item of
Task
structure data type. If you had only one
ArrayList
object and had added both
Projects
and
Tasks
to it, it would not be tabular and it would not be usable for data binding within the
Compact Framework environment.
[Comment 8cs.16]
This requirement for tabular structure also eliminates certain
System.Data
objects. For
instance, a
System.Data.DataTable
or a
System.Data.DataView
is a bindable object
because of its tabular structure. What is
not
bindable in the Compact Framework environment
is the
DataSet
object. This object can contain multiple
DataTables
, tables that can be related
to each other through the use of keys. Figure 8-5 shows a diagram of a
DataSet
object
containing two
DataTable
objects. Because the
DataSet
object is hierarchical in structure,
rather than tabular, it cannot be bound to any control in the Compact Framework environment.
Either
DataTable
could be bound to a control but the entire
DataSet
could not be.
[Comment
8cs.17]
Figure 8-5: A DataSet with two DataTable
s
[Comment 8cs.18]
Also eliminated from data binding in Compact Framework because of their hierarchical
nature are objects from the
System.XML
namespace, such as the
XMLDocument
and the
XMLDataDocument
objects.
[Comment 8cs.19]
To be bindable, a data object must expose its columns as
public properties
, not merely as
fields. This is something to keep in mind when you define your own data structures and object
classes. The structure defined in the previous chapter to hold Task information, shown below,
could not be used in data binding for its data is exposed as fields, not properties.
[Comment
8cs.20]
private
struct
Task
{
public
string
strTaskIdent;
public
string
strTaskName;
public
Date dateTaskStart;
public
Date dateTaskEnd;
public
int
durTaskEstimated;
public
int
durTaskActual;
public
string
strTaskComments;
public
void
Task(
string
strNo,
string
strName,
Date dateStart, Date dateEnd,
int
durEstimated,
int
durActual,
string
strComments)
{
strTaskIdent = strNo;
strTaskName = strName;
dateTaskStart = dateStart;
dateTaskEnd = dateEnd;
durTaskEstimated = durEstimated;
durTaskActual = durActual;
Chapter 8 – Data Binding Page 4
Copyright © 2002-2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
strTaskComments = strComments;
}
}
To make it usable for data binding we must modify it to expose its data as public properties,
like so.
[Comment 8cs.21]
public
struct
Task
{
private
string
m_strIdent;
private
string
m_strProjIdent;
private
string
m_strName;
private
DateTime m_dateStart;
private
DateTime m_dateEnd;
private
int
m_durEstimated;
private
int
m_durActual;
private
string
m_strComments;
public
string
strIdent
{
get
{
return
m_strIdent;
}
set
{
m_strIdent =
value
;
}
}
public
string
strProjIdent
{
get
{
return
m_strProjIdent;
}
set
{
m_strProjIdent =
value
;
}
}
public
string
strName
{
get
{
return
m_strName;
}
set
{
m_strName =
value
;
}
}
public
DateTime dateStart
{
get
{
return
m_dateStart;
Chapter 8 – Data Binding Page 5
Copyright © 2002-2003 Paul Yao & David Durant
Plik z chomika:
Lauviah666
Inne pliki z tego folderu:
AppA_HungarianForDotNETPrograms.pdf
(358 KB)
Ch01_Architecture.pdf
(459 KB)
Ch02_What Is a Compact Framework.pdf
(261 KB)
ch03_The .NET Type System.pdf
(220 KB)
Ch04_Calling Native Functions.pdf
(171 KB)
Inne foldery tego chomika:
C#
daniken
Exchange.2007.-.Ksiega.Eksperta._Berstrong_
Exchange.2007.-.Ksiega.Eksperta._ms_
Piekara_Jacek
Zgłoś jeśli
naruszono regulamin