Wiki NewForum | Latest Entertainment News

Wiki NewForum | Latest Entertainment News (
-   IT Forum (
-   -   GridView (

bholus7 06-01-2009 04:46 PM


DataGridView Control
The DataGridView control is the new grid control for Windows
Froms 2.0. It replaces the DataGrid control with an easy to
use and extremely customizable grid that supports many of
the features that are needed for our customers.

About this document:
This material should not be considered a complete coverage
of DataGridView but it tries to capture the high-level
features with some depth in specific areas.

This document is divided into about 5 logical sections
starting with a feature and structure overview, followed by
an overview of built in column and cell types.

Next is an
overview of working with data, followed by an overview of
specific major features. Lastly, a “best practice” section
concludes the main part of this document.

Most sections contain a “Q & A” section that answers common
questions regarding the specific feature or area covered by
the section.

Note that some questions are duplicated in
multiple sections based upon the section relevancy. The
question and answers with code samples/snippets are
contained in this document’s only appendix to make for a
“one-stop shop” of code.

Note that most of the context of this document comes from
the DataGridView control documentation presented in MSDN.

1 What is the DataGridView 4
1.1 Differences between the DataGridView and DataGrid controls 4
1.2 Highlight of features 5
2 Structure of DGV 6
2.1 Architecture Elements 6
2.2 Cells and Bands 6
2.3 DataGridViewCell 6
2.3.1 How a DataGridViewCell works 7
2.4 DataGridViewColumn 9
2.5 DataGridView Editing Controls 9
2.6 DataGridViewRow 10
3 Out of the box column/cell types 11
3.1 DataGridViewTextBoxColumn 11
3.2 DataGridViewCheckBoxColumn 12
3.3 DataGridViewImageColumn 12
3.4 DataGridView****onColumn 13
3.5 DataGridViewComboBoxColumn 13
3.5.1 DataErrors and the combo box column 13
3.6 DataGridViewLinkColumn 14
4 Working with Data 15
4.1 Data Entry and Validation Events 15
4.1.1 Order of Validation Events 15
4.1.2 Validating Data 15
4.1.3 Data Entry in the New Row 16
4.2 Working with Null values 19
4.2.1 NullValue 19
4.2.2 DataSourceNullValue 19
4.3 DataError event 20
4.4 Databound modes 21
4.4.1 Unbound 21
4.4.2 Bound 21
4.4.3 Virtual 22
4.4.4 Mixed mode – Bound and Unbound 22
5 Overview of features 24
5.1 Styling 24
5.1.1 The DataGridViewCellStyle Class 24
5.1.2 Using DataGridViewCellStyle Objects 24
5.1.3 Style Inheritance 25
5.1.4 Setting Styles Dynamically 28
5.2 Custom painting 28
5.2.1 Paint Parts 28
5.2.2 Row Pre Paint and Post Paint 29
5.3 Autosizing 30
5.3.1 Sizing Options in the Windows Forms DataGridView
Control 30

5.3.2 Resizing with the Mouse 31
5.3.3 Automatic Sizing 32
5.3.4 Programmatic Resizing 33
5.3.5 Customizing Content-based Sizing Behavior 34
5.3.6 Content-based Sizing Options 34
5.4 Selection modes 34
5.4.1 Programmatic Selection 35
5.5 Scrolling 35
5.5.1 Scroll event 35

5.5.2 Scroll bars 35
5.5.3 Scrolling Properties 36
5.6 Sorting 36
5.6.1 Programmatic Sorting 37
5.6.2 Custom Sorting 38
5.7 Border styles 39
5.7.1 Standard Border Styles 39
5.7.2 Advanced Border Styles 39
5.8 Enter-Edit modes 40

5.9 Clipboard copy modes 40
5.10 Frozen columns/rows 41
5.11 Implementing Custom cells and editing controls/cells 41
5.11.1 IDataGridViewEditingControl 42
5.11.2 IDataGridViewEditingCell 42
5.12 Virtual mode 42

5.12.1 Bound Mode and Virtual Mode 42
5.12.2 Supplementing Bound Mode 42
5.12.3 Replacing Bound Mode 43
5.12.4 Virtual-Mode Events 43
5.12.5 Best Practices in Virtual Mode 44
5.13 Capacity 44
6 Best Practices 45
6.1 Using Cell Styles Efficiently 45
6.2 Using Shortcut Menus Efficiently 45

6.3 Using Automatic Resizing Efficiently 45
6.4 Using the Selected Cells, Rows, and Columns
Collections Efficiently 46
6.5 Using Shared Rows 46
6.6 Preventing Rows from Becoming Unshared 47
Appendix A – Common Questions and Answers 49
1. How do I prevent a particular cell from being editable? 49

2. How do I disable a cell? 49
3. How do I restrict user from setting focus to a specific
cell? 51
4. How do I show controls in all cells regardless of edit? 51
5. Why does the cell text show up with “square” characters
where they should be new lines? 51
6. How do I show icon and text in the same cell? 51
7. How do I hide a column? 53

8. How do I prevent the user from sorting on a column? 53
9. How do I sort on multiple columns? 54
10. How do I hook up events on the editing control? 58
11. When should I remove event handlers from the editing
control? 58
12. How do I handle the SelectedIndexChanged event? 58
13. How do I perform drag and drop reorder of rows? 59
14. How do I make the last column wide enough to occupy all
the remaining client area of the grid? 60

15. How do I have the cell text wrap? 60
16. How do I make the image column not show any images? 61
17. How do I enable typing in the combo box cell? 61
18. How do I have a combo box column display a sub set of
data based upon the value of a different combo box column? 61
19. How do I show the error icon when the user is editing
the cell? 62

20. How do I show unbound data along with bound data? 65
21. How do I show data that comes from two tables? 66
22. How do I show master-details? 66
23. How do I show master-details in the same DataGridView? 68
24. How do I prevent sorting? 68

25. How do I commit the data to the database when clicking
on a toolstrip ****on? 68
26. How do I display a confirmation dialog when the user
tries to delete a row? 68

bholus7 06-01-2009 04:48 PM

1 What is the DataGridView
With the DataGridView control, you can display and edit
tabular data from many different kinds of data sources.

The DataGridView control is highly configurable and
extensible, and it provides many properties, methods, and
events to customize its appearance and behavior.

When you
want your Windows Forms application to display tabular data,
consider using the DataGridView control before others (for
example, DataGrid).

If you are displaying a small grid of
read-only values, or if you are enabling a user to edit a
table with millions of records, the DataGridView control
will provide you with a readily programmable,
memory-efficient solution.

The DataGridView control replaces and adds functionality to
the DataGrid control; however, the DataGrid control is
retained for both backward compatibility and future use, if
you choose. See below for details on the differences between
the DataGrid and DataGridView controls.

1.1 Differences between the DataGridView and DataGrid controls
The DataGridView control provides numerous basic and
advanced features that are missing in the DataGrid control.

Additionally, the architecture of the DataGridView control
makes it much easier to extend and customize than the
DataGrid control.

The following table describes a few of the primary features
available in the DataGridView control that are missing from
the DataGrid control.
DataGridView control feature Description
Multiple column types

The DataGridView control provides more
built-in column types than the DataGrid control. These
column types meet the needs of most common scenarios, but
are also easier to extend or replace than the column types
in the DataGrid control.

Multiple ways to display data The DataGrid control is
limited to displaying data from an external data source. The
DataGridView control, however, can display unbound data
stored in the control, data from a bound data source, or
bound and unbound data together.

You can also implement
virtual mode in the DataGridView control to provide custom
data management.

Multiple ways to customize the display of data The
DataGridView control provides many properties and events
that enable you to specify how data is formatted and

For example, you can change the appearance of
cells, rows, and columns depending on the data they contain,
or you can replace data of one data type with equivalent
data of another type.

bholus7 06-01-2009 04:50 PM

Multiple options for changing cell, row, column, and header
appearance and behavior The DataGridView control enables you
to work with individual grid components in numerous ways.

For example, you can freeze rows and columns to prevent them
from scrolling; hide rows, columns, and headers; change the
way row, column, and header sizes are adjusted; change the
way users make selections; and provide ToolTips and shortcut
menus for individual cells, rows, and columns.

The only feature that is available in the DataGrid control
that is not available in the DataGridView control is the
hierarchical display of information from two related tables
in a single control.

You must use two DataGridView controls
to display information from two tables that are in a
master/detail relationship.

1.2 Highlight of features
The following table highlights the DataGridView’s major
features. Further details about a feature can be found later
in this document

DataGridView control feature Description
Multiple column types The DataGridView control provides
TextBox, CheckBox, Image, ****on, ComboBox and Link columns
with the corresponding cell types.

Multiple ways to display data The DataGridView control can
display unbound data stored in the control, data from a
bound data source, or bound and unbound data together.

You can also implement virtual mode in the DataGridView control
to provide custom data management.

Multiple ways to customize the display and work with data
The DataGridView control provides many properties and events
that enable you to specify how data is formatted and displayed.

In addition, the DataGridView control provides multiple ways
to work with your data.

For example, you can:

 sort data with corresponding sort glyph

 enable selection modes by row, column or cell;
multi-selection or single selection

 copy content to the clipboard in multiple formats
including text, CSV (comma separated value) & HTML

 change the way users edit cell content
Multiple options for changing cell, row, column, and header
appearance and behavior

The DataGridView control enables you
to work with individual grid components in numerous ways.

For example, you can:
 freeze rows and columns to prevent them from scrolling
 hide rows, columns, and headers

 change the way row, column, and header sizes are adjusted
based upon size

 change the way users make selections
 provide ToolTips and shortcut menus for individual cells,
rows, and columns

 customize the border styles of cell, rows and columns
Rich extensibility support The DataGridView control provides
the infrastructure to extend and customize the grid. For
example, you can:

 handle custom painting events to provide a custom look and
feel to the cells, columns and rows

 derive from one of the built-in cell types to provide
additional behavior

 implement custom interfaces to create a brand new editing

bholus7 06-01-2009 04:57 PM

2 Structure of DGV
The DataGridView control and its related classes are
designed to be a flexible, extensible system for displaying
and editing tabular data. These classes are all contained in
the System.Windows.Forms namespace, and they are all named
with the "DataGridView" prefix.

2.1 Architecture Elements
The primary DataGridView companion classes derive from

The DataGridViewElement class provides a reference to the
parent DataGridView control and has a State property, which
holds a value that represents a combination of values from
the DataGridViewElementStates enumeration.

2.2 Cells and Bands
The DataGridView control comprises two fundamental kinds of
objects: cells and bands. All cells derive from the
DataGridViewCell base class.

The two kinds of bands,
DataGridViewColumn and DataGridViewRow, both derive from the
DataGridViewBand base class.

The DataGridView control interoperates with several classes,
but the most commonly encountered are DataGridViewCell,
DataGridViewColumn, and DataGridViewRow.

2.3 DataGridViewCell
The cell is the fundamental unit of interaction for the
DataGridView. Display is centered on cells, and data entry
is often performed through cells.

You can access cells by
using the Cells collection of the DataGridViewRow class, and
you can access the selected cells by using the SelectedCells
collection of the DataGridView control.

The DataGridViewCell class diagram
Cell Related Classes and Properties

bholus7 06-01-2009 04:58 PM

The DataGridViewCell type is an abstract base class, from
which all cell types derive. DataGridViewCell and its
derived types are not Windoyws Forms controls, but some host
Windows Forms controls.

Any editing functionality supported
by a cell is typically handled by a hosted control.

DataGridViewCell objects do not control their own appearance
and painting features in the same way as Windows Forms
controls. Instead, the DataGridView is responsible for the
appearance of its DataGridViewCell objects.

You can
significantly affect the appearance and behavior of cells by
interacting with the DataGridView control's properties and

When you have special requirements for
customizations that are beyond the capabilities of the
DataGridView control, you can implement your own class that
derives from DataGridViewCell or one of its child classes.

2.3.1 How a DataGridViewCell works
An important part of understanding the structure of the
DataGridView is to understand how a DataGridViewCell works.

A Cell’s Value
At the root of a cell is its value. For cells in a column
that is not databound and the grid is not in virtual mode
the cells actually store the value in the cell instance. For
databound cells the cell doesn’t “know” or keep the value is
at all.

Anytime the cell’s value is needed the grid goes to
the datasource and looks up the value for the column and row
and returns that as the cell’s value. In virtual mode this
routine is very similar except the grid raises the
CellValueNeeded event to get the cell’s value.

At the cell
level, all of this is controlled via the
DataGridViewCell::GetValue(...) method.
The data type for the cell’s Value property by default is of
type object.

When a column becomes databound its ValueType
property is set which causes each cell’s ValueType to be
updated. The value of the ValueType property is important
for formatting.

bholus7 06-01-2009 04:59 PM

Formatting for Display
Anytime the grid needs to know “how would this cell display”
it needs to get its FormattedValue. This is a complex
routine because formatting something on the screen usually
needs to be converted to a string.

For example, although you
set a cell’s value to the integer value of 155 when 155
needs to be displayed it has to become formatted for the

The cells and column’s FormattedValueType property
determines the type that is used for display. Most columns
use string, but the image and check box cells\columns have
different values. The DataGridViewImageCell and column use
Image as the default FormattedValueType since its painting
code knows how to display an image.

A checkbox cell\column’s
FormattedValueType varies depending upon the value of
ThreeState. At the cell level, all of this is controlled via
the DataGridViewCell::GetFormattedValue(...) method.

By default, the DataGridView uses TypeConverters to convert
a cell’s value to its formatted value. Retrieving the proper
TypeConverter is based upon the cell’s ValueType and
FormattedValueType properties.

For a cell, the FormattedValue is requested many times.
Anytime the cell is painted or when a column needs to be
autosized based upon the cell’s content; the FormattedValue
is even needed when determining if the mouse is over the
cell content or not.

Anytime the FormattedValue is required
the DataGridView raises the CellFormatting event. This
provides you with the opportunity to modify how the cell is

If a cell cannot retrieve its formatted value correctly it
raises the DataError event.
Part of formatting a cell for display is understanding what
the preferred size of the cell is.

The preferred size is a
combination of the cell’s FormattedValue, any padding or
additional display and the borders.

Painting the Display
After the FormattedValue is retrieved the cell’s responsible
for painting the cell’s content. The cell determines the
correct style to paint with (see the Styling section later
in this document) and paints the cell.

It is important to
note that if a cell does not paint itself then nothing is
painted. A row or column performs no painting, so ensure
that at least a background is painted in the cell otherwise
the rectangle remains invalidated (unpainted).

All times are GMT. The time now is 04:12 PM.

Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2020, vBulletin Solutions, Inc.