Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

For our purposes, we will continue to use the typed IList interface supported by our PhotoAlbum class. Later in the chapter we will add support for the IEditableObject interface in order to properly save modifications made in our data grid.

The next section discusses various ways of customizing what appears in the grid.

17.2DATA GRID CUSTOMIZATION

One of the obvious drawbacks of letting .NET do all the work in laying out the contents of a data grid is that we have no control over the selection and order of columns to appear in the grid. In this section we will look at how to customize the contents of a data grid for a particular data source using table and column styles. This will enable us to build the application shown in figure 17.4.

Figure 17.4 This data grid displays only certain properties of photographs, and the size and content of each column are somewhat customized compared with the application in the previous section.

In our current application we display a single kind of table, namely one based on the PhotoAlbum class. In general, the data displayed in a data grid may vary depending on the actions of the user. For example, just as our ListView control in chapter 14 displayed both albums and photographs, we could create an AlbumCollection class derived from CollectionBase to contain the set of albums located in a given directory. We could then use this class to display both album files and the contents of albums in our data grid.

More commonly, a data grid is filled with information from a database, which includes one or more tables. An employee database at a company might have one table containing department information, another table containing the employees assigned to each department, and another containing the projects each employee is assigned to. A single data grid could display all three types of tables based on a set of options, and it would be nice to customize the appearance of each type of table.

DATA GRID CUSTOMIZATION

573

The TableStyles property in the DataGrid class supports this notion of configuring the appearance of multiple tables. This property contains a collection of DataGridTableStyle objects, each of which describes the configuration for a table that might be displayed by the grid. The DataGridTableStyle class, in turn, provides a

GridColumnStyles property that contains a collection of DataGridColumnStyle objects. We will discuss each of these classes separately.

17.2.1CUSTOMIZING TABLE STYLES

The DataGridTableStyle class permits a custom style for a specific type of table to be defined. Many of the members of this class are duplicates of similar members in the DataGrid class. The members of the active table style always override the default settings for the data grid. A summary of this class appears in .NET Table 17.2.

.NET Table 17.2 DataGridTableStyle class

The DataGridTableStyle class represents the style in which to display a particular table that can appear in a DataGrid control. It configures not only the general properties for the table but also the individual columns that should appear in the table. This class is part of the System.Windows.Forms namespace, and inherits from the System.ComponentModel.Component class.

 

AllowSorting

Gets or sets whether sorting is allowed on the grid when this

 

 

DataGridTableStyle is used.

 

AlternatingBack-

Gets or sets the background color for alternating rows in the

 

Color

grid when this DataGridTableStyle is used.

 

DataGrid

Gets or sets the DataGrid control containing this style.

 

GridColumn-

Gets or sets the collection of DataGridColumnStyle objects

 

Styles

to use for the grid when this style is used.

 

LinkColor

Gets or sets the color of link text to use in the grid when this

Public

 

style is used.

 

 

Properties

MappingName

Gets or sets the name used to associate this table style with

 

 

 

a specific data source. For a data source based on an IList

 

 

interface, the name of the list is specified, as in

 

 

myList.GetType().Name. For a data source based on a

 

 

DataSet instance, a valid table name in the data set should

 

 

be specified.

 

ReadOnly

Gets or sets whether columns can be edited in the grid when

 

 

this style is used.

 

RowHeader-

Gets or sets the width of row headers in the grid when this

 

Width

style is used.

 

 

 

 

BeginEdit

Requests an edit operation on a row in the grid.

Public

EndEdit

Requests an end to an edit operation in the grid.

Methods

ResetBackColor

Resets the BackColor property to its default value. A

 

 

 

number of other reset methods exist with a similar purpose.

 

 

 

Public

AllowSorting-

Occurs when the AllowSorting property value changes. A

Events

Changed

number of other changed events exist with a similar purpose.

 

 

 

574

CHAPTER 17 DATA BINDING

There are two keys to understanding the DataGridTableStyle class. The first is the MappingName property. When a new source of data is assigned to a DataGrid control, the list of table styles is examined to locate a style whose MappingName setting matches the name of the table. If one is found, then that style is used to display the grid. If no match is found, then the default settings for the grid control are used. It is an error to assign identical mapping names to multiple styles within the same data grid.

The second key to understanding this class is the GridColumnStyles property. This property is a collection of DataGridColumnStyle objects and specifies the selection and order of columns to display in the grid. If the GridColumnStyles property is null, then the default set of columns is displayed.

We can use the DataGridTableStyle class to modify the appearance of our DataGrid control when a PhotoAlbum is displayed. We will make the very simple change of providing an alternating background color for the table. The steps required are presented in the following table.

Set the version number of the MyAlbumData application to 17.2

PROVIDE A CUSTOM TABLE STYLE WHEN A PHOTOALBUM IS DISPLAYED

 

Action

Result

 

 

 

1

In the MainForm.cs code

protected override void OnLoad(EventArgs e)

 

window, create a table style

{

 

instance in the OnLoad method.

. . .

 

// Table style for PhotoAlbum data source

 

 

 

Note: A table style can also be

DataGridTableStyle albumStyle

 

created in the [Design] window

= new DataGridTableStyle();

 

 

 

by clicking on the button in

 

 

the TableStyles property.

 

 

Here we elect to create the

 

 

table style by hand.

 

 

 

 

2

Configure the new style for a

albumStyle.MappingName = "PhotoAlbum";

 

PhotoAlbum table with an

albumStyle.AlternatingBackColor

 

alternating background color of

= Color.LightGray;

 

albumStyle.RowHeaderWidth = 15;

 

LightGray.

 

 

 

 

 

3

Assign the new style to the

// Assign the table style to the data grid

 

existing DataGrid control.

gridPhotoAlbum.TableStyles.Add(albumStyle);

 

 

}

 

 

 

This very simple change causes the application to display as is shown in figure 17.5. Of course, the AlternatingBackColor and RowHeaderWidth properties are available in the DataGrid class and can be set explicitly for this class. Assigning them in a table style uses these properties only when a matching table name is displayed, in this case a PhotoAlbum object.

Note that our choice of light gray may not work very well with some user’s desktop colors. You can use an alternate color if you prefer, or a system color such as SystemColors.ControlLight. In your own applications, make sure you carefully select

DATA GRID CUSTOMIZATION

575

color choices for settings such as this, and use system settings where possible. Hardcoding a specific color such as we do here is not typically recommended, since different users may configure their desktops to appear using different sets of conflicting colors.

Figure 17.5 This figure shows an alternating background color of light gray to present a ledger-like appearance.

Note that the assignment of the MappingName is critical here. Using a name other than PhotoAlbum would have no effect on our table since the name of the table would not match the mapping name of the table style.

Of course, our table still uses the default set of columns since we have not yet assigned any column styles to the GridColumnStyles property. Customizing the columns in our table is our next topic.

17.2.2CUSTOMIZING COLUMN STYLES

Now that we know how to customize the properties of a table, let’s look at how to customize the columns that appear in the table. The DataGridColumnStyle class is used for this purpose, and is summarized in .NET Table 17.3. This is an abstract class from which various types of columns are derived. The .NET Framework currently provides classes to represent boolean and text columns, namely the DataGridBoolColumn and DataGridTextBoxColumn classes.

576

CHAPTER 17 DATA BINDING

.NET Table 17.3 DataGridColumnStyle class

The DataGridColumnStyle class represents a specific column that should appear when a specific style table is displayed in a DataGrid control. This object is typically contained within a DataGridTableStyle object, and indicates the position and style for the corresponding column when a table of the specified type is displayed. This class is part of the System.Windows.Forms namespace, and inherits from the System.ComponentModel.Component class. A DataGridColumnStyle object cannot be instantiated, as this is an abstract class. The

DataGridBoolColumn and DataGridTextBoxColumn classes derived from this class are used to represent a column of boolean or textual values, respectively. Custom column styles derived from this class may also be created.

 

Alignment

Gets or sets the alignment of data within the column.

 

DataGridTableStyle

Gets the table style containing this column style.

 

HeaderText

Gets or sets the header text for this column when the

 

 

associated table style is used.

 

MappingName

Gets or sets the name used to associate this column

 

 

style with a specific data value in an associated data

 

 

source. For an IList data source, a valid property name

Public

 

in the list should be specified. For a DataSet data

 

source, a valid column name in the associated table

Properties

 

 

should be provided.

 

NullText

Gets or sets the text that is displayed when the column

 

 

contains a null reference.

 

PropertyDescriptor

Gets or sets the PropertyDescriptor object

 

 

containing attributes of the data displayed by this

 

 

column style.

 

ReadOnly

Gets or sets whether to treat the column as read-only.

 

Width

Gets or sets the width in pixels for this column.

 

 

 

Public

ResetHeaderText

Resets the HeaderText property to its default value,

Methods

 

which is a null reference.

 

 

 

 

AlignmentChanged

Occurs when the Alignment property for the column

Public

 

style changes.

 

 

Events

FontChanged

Occurs when the column’s font changes. A number of

 

 

 

other changed events exist with a similar purpose.

 

 

 

The order in which columns are assigned to a table style determines the order in which they will appear in the data grid. We will use this feature to extend the table style we created for our form to display only a subset of the available columns.

The code to make this change is detailed in the following table. Note that this code uses the DataGridBoolColumn and DataGridTextBoxColumn classes. We will discuss these classes in more detail in a moment.

DATA GRID CUSTOMIZATION

577

Соседние файлы в папке c#