DataGridView FAQ
DataGridView FAQ
doc
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.
Conten ts
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..........................................................................................................12
3.2 DataGridViewCheckBoxColumn.......................................................................................................12
3.3 DataGridViewImageColumn..............................................................................................................12
3.4 DataGridViewButtonColumn.............................................................................................................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
Page 1 of 69
2477405.doc
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.....................................................................................................................................36
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.........................................................................................................................41
5.10 Frozen columns/rows........................................................................................................................41
5.11 Implementing Custom cells and editing controls/cells...................................................................42
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
Page 2 of 69
2477405.doc
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 button?..........................68
26. How do I display a confirmation dialog when the user tries to delete a row?................................68
Page 3 of 69
2477405.doc
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.
The following table describes a few of the primary features available in the DataGridView control that
are missing from the DataGrid control.
The DataGridView control provides more built-in column types than the
DataGrid control. These column types meet the needs of most common
Multiple column types
scenarios, but are also easier to extend or replace than the column types in
the DataGrid control.
The DataGridView control provides many properties and events that enable
you to specify how data is formatted and displayed. For example, you can
Multiple ways to customize the
change the appearance of cells, rows, and columns depending on the data
display of data
they contain, or you can replace data of one data type with equivalent data
of another type.
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
Page 4 of 69
2477405.doc
use two DataGridView controls to display information from two tables that are in a master/detail
relationship.
The DataGridView control can display unbound data stored in the control,
data from a bound data source, or bound and unbound data together. You
Multiple ways to display data
can also implement virtual mode in the DataGridView control to provide
custom data management.
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:
Multiple ways to customize the sort data with corresponding sort glyph
display and work with data 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
Page 5 of 69
2477405.doc
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.
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.
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.
Page 6 of 69
2477405.doc
The Da ta Grid ViewC ell clas s diagr am Cell Rel ated Clas ses and Pro pe rties
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 events. 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.
A Cel l’ s Valu e
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.
Page 7 of 69
2477405.doc
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
display. 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 formatted.
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.
Par si ng th e Di sp lay
After the user interacts with a cell at some point the user will edit a cell’s value. One
important thing to note is that the user in reality is editing the cell’s FormattedValue. When
committing the value the FormattedValue has to be converted back to the cell’s value. This is
called parsing. At the cell level, all of this is controlled via the DataGridViewCell::
ParseFormattedValue (int rowIndex) method.
By default, TypeConverters are used again to parse the formatted value to the real value. The
DataGridView raises the CellParsing event at this time to provide you with the opportunity to
modify how the cell’s formatted value is parsed.
If a cell cannot correctly parse the formatted value it raises the DataError event.
Page 8 of 69
2477405.doc
2.4 DataGridViewColumn
The schema of the DataGridView control's attached data store is expressed in the DataGridView
control's columns. You can access the DataGridView control's columns by using the Columns
collection. You can access the selected columns by using the SelectedColumns collection.
The Da ta Grid ViewCol umn class Column Rel ated Clas ses and Pro pe rties
diag ra m
Some of the key cell types have corresponding column types. These are derived from the
DataGridViewColumn base class.
The following editing controls are provided with the DataGridView control:
The following table illustrates the relationship among cell types, column types, and editing controls.
Cell type Hosted control Column type
Page 9 of 69
2477405.doc
2.6 DataGridViewRow
The DataGridViewRow class displays a record's data fields from the data store to which the
DataGridView control is attached. You can access the DataGridView control's rows by using the Rows
collection. You can access the selected rows by using the SelectedRows collection.
The Da ta Grid ViewCol umn class Row R elat ed Classe s and P rop erti es
diag ra m
You can derive your own types from the DataGridViewRow class, although this will typically not be
necessary. The DataGridView control has several row-related events and properties for customizing
the behavior of its DataGridViewRow objects.
If you enable the DataGridView control's AllowUserToAddRows property, a special row for adding
new rows appears as the last row. This row is part of the Rows collection, but it has special
functionality that may require your attention. For more information, see Using the Row for New
Records in the Windows Forms DataGridView Control.
Page 10 of 69
2477405.doc
When you bind a DataGridView control and set the AutoGenerateColumns property to true, columns
are automatically generated using default column types appropriate for the data types contained in the
bound data source.
You can also create instances of any of the column classes yourself and add them to the collection
returned by the Columns property. You can create these instances for use as unbound columns, or you
can manually bind them. Manually bound columns are useful, for example, when you want to replace
an automatically generated column of one type with a column of another type.
The following table describes the various column classes available for use in the DataGridView control:
Class Description
You can create your own column class by inheriting the DataGridViewColumn
class or any of its derived classes to provide custom appearance, behavior, or
Your custom column type hosted controls. For more information, see How to: Customize Cells and
Columns in the Windows Forms DataGridView Control by Extending
Their Behavior and Appearance
Page 11 of 69
2477405.doc
3.1 DataGridViewTextBoxColumn
The DataGridViewTextBoxColumn is a general-purpose column type for use with text-based values
such as numbers and strings. In editing mode, a TextBox control is displayed in the active cell, enabling
users to modify the cell value.
Cell values are automatically converted to strings for display. Values entered or modified by the user
are automatically parsed to create a cell value of the appropriate data type. You can customize these
conversions by handling the CellFormatting and CellParsing events of the DataGridView control.
The cell value data type of a column is specified in the ValueType property of the column.
3.2 DataGridViewCheckBoxColumn
The DataGridViewCheckBoxColumn is used with Boolean and CheckState values. Boolean values
display as two-state or three-state check boxes, depending on the value of the ThreeState property.
When the column is bound to CheckState values, the ThreeState property value is true by default.
Typically, check box cell values are intended either for storage, like any other data, or for performing
bulk operations. If you want to respond immediately when users click a check box cell, you can handle
the CellClick event, but this event occurs before the cell value is updated. If you need the new value at
the time of the click, one option is to calculate what the expected value will be based on the current
value. Another approach is to commit the change immediately, and handle the CellValueChanged
event to respond to it. To commit the change when the cell is clicked, you must handle the
CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the
CommitEdit method and pass in the Commit value.
3.3 DataGridViewImageColumn
The DataGridViewImageColumn is used to display images. Image columns can be populated
automatically from a data source, populated manually for unbound columns, or populated dynamically
in a handler for the CellFormatting event.
The automatic population of an image column from a data source works with byte arrays in a variety of
image formats, including all formats supported by the Image class and the OLE Picture format used by
Microsoft® Access and the Northwind sample database.
Populating an image column manually is useful when you want to provide the functionality of a
DataGridViewButtonColumn, but with a customized appearance. You can handle the CellClick event
to respond to clicks within an image cell.
Populating the cells of an image column in a handler for the CellFormatting event is useful when you
want to provide images for calculated values or values in non-image formats. For example, you may
Page 12 of 69
2477405.doc
have a "Risk" column with string values such as "high", "middle", and "low" that you want to display as
icons. Alternately, you may have an "Image" column that contains the locations of images that must be
loaded rather than the binary content of the images.
3.4 DataGridViewButtonColumn
With the DataGridViewButtonColumn, you can display a column of cells that contain buttons. This is
useful when you want to provide an easy way for your users to perform actions on particular records,
such as placing an order or displaying child records in a separate window.
Button columns are not generated automatically when data-binding a DataGridView control. To use
button columns, you must create them manually and add them to the collection returned by the
Columns property.
You can respond to user clicks in button cells by handling the CellClick event.
3.5 DataGridViewComboBoxColumn
With the DataGridViewComboBoxColumn, you can display a column of cells that contain drop-down
list boxes. This is useful for data entry in fields that can only contain particular values, such as the
Category column of the Products table in the Northwind sample database.
You can populate the drop-down list used for all cells the same way you would populate a ComboBox
drop-down list, either manually through the collection returned by the Items property, or by binding
it to a data source through the DataSource, DisplayMember, and ValueMember properties. For more
information, see ComboBox Control (Windows Forms).
You can bind the actual cell values to the data source used by the DataGridView control by setting the
DataPropertyName property of the DataGridViewComboBoxColumn.
Combo box columns are not generated automatically when data-binding a DataGridView control. To
use combo box columns, you must create them manually and add them to the collection returned by
the Columns property. Alternatively you can use the designer and change a column type to a combo
box column and set properties accordingly.
Page 13 of 69
2477405.doc
3.6 DataGridViewLinkColumn
With the DataGridViewLinkColumn, you can display a column of cells that contain hyperlinks. This is
useful for URL values in the data source or as an alternative to the button column for special behaviors
such as opening a window with child records.
Link columns are not generated automatically when data-binding a DataGridView control. To use link
columns, you must create them manually and add them to the collection returned by the Columns
property.
You can respond to user clicks on links by handling the CellContentClick event. This event is distinct
from the CellClick and CellMouseClick events, which occur when a user clicks anywhere in a cell.
The DataGridViewLinkColumn class provides several properties for modifying the appearance of links
before, during, and after they are clicked.
Page 14 of 69
2477405.doc
4.1.2Validating Data
When validating user data, you will usually validate the data at the cell level when the grid is not
databound, and at the row level when the grid is databound. Sometimes when databound you will
perform validation both at the cell and row level.
4.1.2.1 Displaying error information
When you do encounter data that is invalid you usually notify the user about it. There are many ways
to do this; the conventional way is to use a message box. The DataGridview has the ability to show an
error icon for the cell and rows to indicate that invalid data was entered. The error icon has a tooltip
associated with it to provide informational about what is in error:
Page 15 of 69
2477405.doc
For the data bound case, the row for new records will be shown if the AllowUserToAddRows property
of the control and the IBindingList.AllowNew property of the data source are both true. If either is
false then the row will not be shown.
Page 16 of 69
2477405.doc
4.1.3.2 Populating the Row for New Records with Default Data
When the user selects the row for new records as the current row, the DataGridView control raises the
DefaultValuesNeeded event. This event provides access to the new DataGridViewRow and enables you
to populate the new row with default data.
The following code example demonstrates how to specify default values for new rows using the
DefaultValuesNeeded event.
private void dataGridView1_DefaultValuesNeeded(object sender,
DataGridViewRowEventArgs e)
{
e.Row.Cells["Region"].Value = "WA";
e.Row.Cells["City"].Value = "Redmond";
e.Row.Cells["PostalCode"].Value = "98052-6399";
e.Row.Cells["Region"].Value = "NA";
e.Row.Cells["Country"].Value = "USA";
e.Row.Cells["CustomerID"].Value = NewCustomerId();
}
Even though the new row is in the rows collection it does behaves differently in two respects:
The row for new records cannot be removed from the Rows collection programmatically. An
InvalidOperationException is thrown if this is attempted. The user also cannot delete the row
for new records. The DataGridViewRowCollection.Clear method does not remove this row
from the Rows collection.
No row can be added after the row for new records. An InvalidOperationException is raised if
this is attempted. As a result, the row for new records is always the last row in the
DataGridView control. The methods on DataGridViewRowCollection that add rows—Add,
AddCopy, and AddCopies—all call insertion methods internally when the row for new
records is present.
Page 17 of 69
2477405.doc
When this “new” new row is added the UserAddedRow event fires with the Row event args property
identifying the “new” new row. If the user hits the Escape key at this stage the “new” new row is
removed. This causes the UserDeletingRow event to fire with the Row event args property identifying
the “new” new row.
The initial values displayed by cells in the row for new records are retrieved from each cell's
DefaultNewRowValue property. For cells of type DataGridViewImageCell, this property returns a
placeholder image. Otherwise, this property returns null. You can override this property to return a
custom value. However, these initial values can be replaced by a DefaultValuesNeeded event handler
when focus enters the row for new records.
The standard icons for this row's header, which are an arrow or an asterisk, are not exposed publicly. If
you want to customize the icons, you will need to create a custom DataGridViewRowHeaderCell class.
The standard icons use the ForeColor property of the DataGridViewCellStyle in use by the row header
cell. The standard icons are not rendered if there is not enough space to display them completely.
If the row header cell has a string value set, and if there is not enough room for both the text and icon,
the icon is dropped first.
In data bound and virtual modes, the insertion behavior when a sort is applied will be dependent on
the implementation of the data model. For ADO.NET, the row is immediately sorted into the correct
position.
Page 18 of 69
2477405.doc
The row for new records is always created in the unselected state.
4.2.1NullValue
The DataGridViewCellStyle.NullValue property should have been called FormattedNullValue, but it
was too late to make this change. Hopefully this provides a clue into how the NullValue is used – at
formatting time. When a cell’s value is “null” (equal to Null or DBNull.Value) the value in the
DataGridViewCellStyle.NullValue property is used for display. The default value of this property is
based upon the column:
DataGridView column Column’s DefaultCellStyle.NullValue
4.2.2DataSourceNullValue
The DataGridViewCellStyle.DataSourceNullValue property could have been called ParseNullValue if
the other property was FormattedNullValue, but in the end DataSourceNullValue made good sense.
Page 19 of 69
2477405.doc
The DataSourceNullValue property is used when writing the value of “Null” to the cell’s value. In
databound scenarios this value gets written to the database or business object. This is important to
control as business objects and databases have different concepts of null. Usually you’ll want to set
DataSourceNullValue to null when working with business objects and DBNull.Value when working
with databases. The default value of DataSourceNullValue is DBNull.Value.
When attempting to paint the cell or calculate the cell's tooltiptext. Note
Display that these operations usually also require getting the cell's formatted
value, so the error context is OR'd together.
LeaveControl When exceptions occur while attempting to validate grid data when the
Page 20 of 69
2477405.doc
Unbound mode can be particularly useful for static, read-only data, or when you want to provide your
own code that interacts with an external data store. When you want your users to interact with an
external data source, however, you will typically use bound mode.
4.4.2Bound
Bound mode is suitable for managing data using automatic interaction with the data store. You can
attach the DataGridView control directly to its data source by setting the DataSource property. When
the control is data bound, data rows are pushed and pulled without the need of explicit management
on your part. When the AutoGenerateColumns property is true, each column in your data source will
cause a corresponding column to be created in the control. If you prefer to create your own columns,
you can set this property to false and use the DataPropertyName property to bind each column when
you configure it. This is useful when you want to use a column type other than the types that are
generated by default. For more info on databinding the DataGridView, check out the following MSDN
article: http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnforms/html/winforms11162004.asp Also, check out the DataBinding FAQ posted at
WindowsForms.net
The DataGridView control supports the standard Windows Forms data binding model, so it will bind
to instances of classes described in the following list:
• Any class that implements the IList interface, including one-dimensional arrays.
• Any class that implements the IListSource interface, such as the DataTable and DataSet
classes.
Page 21 of 69
2477405.doc
• Any class that implements the IBindingList interface, such as the BindingList class.
• Any class that implements the IBindingListView interface, such as the BindingSource
class.
Li st Cha ng e Noti fica tio n
One of the most important parts when databinding is for the list to support change notifications. This is
only important if you want the DataGridView to be updated when the list changes such as add, updates
and deletes. Only datasources that implement the IBindingList support change notification. Lists such
as arrays or collections do not support change notification by default.
The BindingSource component is the preferred data source because it can bind to a wide variety of data
sources and can resolve many data binding issues automatically. Typically, you will bind to a
BindingSource component and bind the BindingSource component to another data source or populate
it with business objects. The BindingList<T> class can also be used to create a custom list based upon a
type.
4.4.3Virtual
With virtual mode, you can implement your own data management operations. This is necessary to
maintain the values of unbound columns in bound mode when the control is sorted by bound columns.
The primary use of virtual mode, however, is to optimize performance when interacting with large
amounts of data.
You attach the DataGridView control to a cache that you manage, and your code controls when data
rows are pushed and pulled. To keep the memory footprint small, the cache should be similar in size to
the number of rows currently displayed. When the user scrolls new rows into view, your code requests
new data from the cache and optionally flushes old data from memory.
When you are implementing virtual mode, you will need to track when a new row is needed in the
data model and when to rollback the addition of the new row. The exact implementation of this
functionality will depend on the implementation of the data model and the transaction semantics of
the data model; whether commit scope is at the cell or row level.
For more info about Virtual mode, see the “Virtual Mode” topic later in this document.
You can add unbound columns to a DataGridView control in bound mode. This is useful when you
want to display a column of buttons or links that enable users to perform actions on specific rows. It is
also useful to display columns with values calculated from bound columns. You can populate the cell
Page 22 of 69
2477405.doc
values for calculated columns in a handler for the CellFormatting event. If you are using a DataSet or
DataTable as the data source, however, you might want to use the DataColumn.Expression property to
create a calculated column instead. In this case, the DataGridView control will treat calculated column
just like any other column in the data source.
Sorting by unbound columns in bound mode is not supported. If you create an unbound column in
bound mode that contains user-editable values, you must implement virtual mode to maintain these
values when the control is sorted by a bound column.
Virtual mode should also be used in mix mode when the additional data being displayed cannot be
calculated based upon the data that the grid is bound to or when the data is updated frequently. For
more info about Virtual mode, see the “Virtual Mode” topic later in this document.
Page 23 of 69
2477405.doc
5 Overview of features
5.1 Styling
The DataGridView control makes it easy to define the basic appearance of cells and the display
formatting of cell values. You can define appearance and formatting styles for individual cells, for cells
in specific columns and rows, or for all cells in the control by setting the properties of the
DataGridViewCellStyle objects accessed through various DataGridView control properties.
Additionally, you can modify these styles dynamically based on factors such as the cell value by
handling the CellFormatting event.
Each cell within the DataGridView control can have its own style, such as text format, background
color, foreground color, and font. Typically, however, multiple cells will share particular style
characteristics.
Groups of cells that share styles may include all cells within particular rows or columns, all cells that
contain particular values, or all cells in the control. Because these groups overlap, each cell may get its
styling information from more than one place. For example, you may want every cell in a
DataGridView control to use the same font, but only cells in currency columns to use currency format,
and only currency cells with negative numbers to use a red foreground color.
You can avoid unnecessary duplication of style information by sharing DataGridViewCellStyle objects
among multiple DataGridView elements. Because the styles set at the control, column, and row levels
filter down through each level to the cell level, you can also avoid style duplication by setting only
those style properties at each level that differ from the levels above. This is described in more detail in
the Style Inheritance section that follows.
The following table lists the primary properties that get or set DataGridViewCellStyle objects.
Property Classes Description
DefaultCellStyle DataGridView, Gets or sets default styles used by all cells in the
DataGridViewColumn, entire control (including header cells), in a column, or
Page 24 of 69
2477405.doc
DataGridViewRow,
in a row.
and derived classes
DataGridViewCell and Gets or sets styles specified at the cell level. These
Style
derived classes styles override those inherited from higher levels.
DataGridViewCell,
DataGridViewRow, Gets all the styles currently applied to the cell, row, or
InheritedStyle
DataGridViewColumn, column, including styles inherited from higher levels.
and derived classes
As mentioned above, getting the value of a style property automatically instantiates a new
DataGridViewCellStyle object if the property has not been previously set. To avoid creating these
objects unnecessarily, the row and column classes have a HasDefaultCellStyle property that you can
check to determine whether the DefaultCellStyle property has been set. Similarly, the cell classes have
a HasStyle property that indicates whether the Style property has been set.
Each of the style properties has a corresponding PropertyNameChanged event on the DataGridView
control. For row, column, and cell properties, the name of the event begins with "Row", "Column", or
"Cell" (for example, RowDefaultCellStyleChanged). Each of these events occurs when the
corresponding style property is set to a different DataGridViewCellStyle object. These events do not
occur when you retrieve a DataGridViewCellStyle object from a style property and modify its property
values. To respond to changes to the cell style objects themselves, handle the CellStyleContentChanged
event.
5.1.3Style Inheritance
Each DataGridViewCell gets its appearance from its InheritedStyle property. The
DataGridViewCellStyle object returned by this property inherits its values from a hierarchy of
properties of type DataGridViewCellStyle. These properties are listed below in the order in which the
InheritedStyle for non-header cells obtains its values.
1. DataGridViewCell.Style
2. DataGridViewRow.DefaultCellStyle
3. AlternatingRowsDefaultCellStyle (only for cells in rows with odd index numbers)
4. RowsDefaultCellStyle
5. DataGridViewColumn.DefaultCellStyle
6. DefaultCellStyle
For row and column header cells, the InheritedStyle property is populated by values from the
following list of source properties in the given order.
Page 25 of 69
2477405.doc
1. DataGridViewCell.Style
2. ColumnHeadersDefaultCellStyle or RowHeadersDefaultCellStyle
3. DefaultCellStyle
You can also access the styles inherited by specific rows and columns. The column InheritedStyle
property inherits its values from the following properties.
1. DataGridViewColumn.DefaultCellStyle
2. DefaultCellStyle
The row InheritedStyle property inherits its values from the following properties.
1. DataGridViewRow.DefaultCellStyle
2. AlternatingRowsDefaultCellStyle (only for cells in rows with odd index numbers)
3. RowsDefaultCellStyle
4. DefaultCellStyle
The following table illustrates how the ForeColor property value for an example cell is inherited from
its containing column.
Example ForeColor value for retrieved
Property of type DataGridViewCellStyle
object
DataGridViewCell.Style Color.Empty
DataGridViewRow.DefaultCellStyle Color.Red
AlternatingRowsDefaultCellStyle Color.Empty
Page 26 of 69
2477405.doc
RowsDefaultCellStyle Color.Empty
DataGridViewColumn.DefaultCellStyle Color.DarkBlue
DefaultCellStyle Color.Black
In this case, the System.Drawing.Color.Red value from the cell's row is the first real value on the list.
This becomes the ForeColor property value of the cell's InheritedStyle.
The following diagram illustrates how different DataGridViewCellStyle properties can inherit their
values from different places.
By taking advantage of style inheritance, you can provide appropriate styles for the entire control
without having to specify the same information in multiple places.
Although header cells participate in style inheritance as described, the objects returned by the
ColumnHeadersDefaultCellStyle and RowHeadersDefaultCellStyle properties of the DataGridView
control have initial property values that override the property values of the object returned by the
DefaultCellStyle property. If you want the properties set for the object returned by the
DefaultCellStyle property to apply to row and column headers, you must set the corresponding
properties of the objects returned by the ColumnHeadersDefaultCellStyle and
RowHeadersDefaultCellStyle properties to the defaults indicated for the DataGridViewCellStyle class.
Note : If visual styles are enabled, the row and column headers (except for the
TopLeftHeaderCell) are automatically styled by the current theme, overriding any styles
specified by these properties. Set the EnableHeadersVisualStyle property to false if you want
headers to not use XP’s visual styles.
Page 27 of 69
2477405.doc
You can also dynamically modify the styles of individual cells in response to events such as the
CellMouseEnter and CellMouseLeave events. For example, in a handler for the CellMouseEnter event,
you could store the current value of the cell background color (retrieved through the cell's Style
property), then set it to a new color that will highlight the cell when the mouse hovers over it. In a
handler for the CellMouseLeave event, you can then restore the background color to the original value.
Note : Caching the values stored in the cell's Style property is important regardless of whether
a particular style value is set. If you temporarily replace a style setting, restoring it to its
original "not set" state ensures that the cell will go back to inheriting the style setting from a
higher level. If you need to determine the actual style in effect for a cell regardless of whether
the style is inherited, use the cell's InheritedStyle property.
5.2.1Paint Parts
One important part of custom painting is the concept of paint parts. The DataGridViewPainParts
enumeration is used to specify what parts a cell paints. Enum values can be combined together to have
a cell paint or not paint specific parts. Here are the different parts:
PaintPart Example ForeColor value for retrieved object
Background The background of the cell is painted using the cell’s background color (1)
Page 28 of 69
2477405.doc
Not es
1) If a cell does not paint its background then nothing is painted. A row or column performs no
painting, so ensure that at least the cell’s background is painted or you perform your own
custom background painting; otherwise the rectangle remains invalidated (unpainted).
2) Each cell determines what it paints as content foreground and content background as
described by the following list:
Cell Type Content Foreground Content Background
Alternately, you can let the cells paint themselves and add custom foreground content in a handler for
the DataGridView.RowPostPaint event. You can also disable cell painting and paint everything
yourself in a DataGridView.RowPrePaint event handler
Page 29 of 69
2477405.doc
5.3 Autosizing
The DataGridView control provides numerous options for customizing the sizing behavior of its
columns and rows. Typically, DataGridView cells do not resize based on their contents. Instead, they
clip any display value that is larger than the cell. If the content can be displayed as a string, the cell
displays it in a ToolTip.
By default, users can drag row, column, and header dividers with the mouse to show more information.
Users can also double-click a divider to automatically resize the associated row, column, or header
band based on its contents. Columns share the available width of the control by default, so if users can
resize the control—for example, if it is docked to a resizable form—they can also change the available
display space for all columns.
The DataGridView control provides properties, methods, and events that enable you to customize or
disable all of these user-directed behaviors. Additionally, you can programmatically resize rows,
columns, and headers to fit their contents, or you can configure them to automatically resize
themselves whenever their contents change.
User resize Users can make size adjustments by dragging or double-clicking row, column, or header dividers.
In column fill mode, column widths change when the control width changes; for example, when the
Control resize
control is docked to its parent form and the user resizes the form.
Cell value
In content-based automatic sizing modes, sizes change to fit new display values.
change
Programmatic content-based resizing lets you make opportunistic size adjustments based on cell
Method call
values at the time of the method call.
Property
You can also set specific height and width values.
setting
By default, user resizing is enabled, automatic sizing is disabled, and cell values that are wider than
their columns are clipped.
The following table shows scenarios that you can use to adjust the default behavior or to use specific
sizing options to achieve particular effects.
Scenario Implementation
Use column fill mode for displaying similarly sized data in Set the AutoSizeColumnsMode property to Fill.
a relatively small number of columns that occupy the
Page 30 of 69
2477405.doc
Use column fill mode to avoid displaying the control Set the AutoSizeMode property of the last column to Fill
background. and use other sizing options for the other columns.
Page 31 of 69
2477405.doc
• AllowUserToResizeColumns
• AllowUserToResizeRows
• ColumnHeadersHeightSizeMode
• RowHeadersWidthSizeMode
You can also prevent users from resizing individual rows or columns by setting their Resizable
properties. By default, the Resizable property value is based on the AllowUserToResizeColumns
property value for columns and the AllowUserToResizeRows property value for rows. If you explicitly
set Resizable to True or False, however, the specified value overrides the control value is for that row
or column. Set Resizable to NotSet to restore the inheritance.
Because NotSet restores the value inheritance, the Resizable property will never return a NotSet value
unless the row or column has not been added to a DataGridView control. If you need to determine
whether the Resizable property value of a row or column is inherited, examine its State property. If the
State value includes the ResizableSet flag, the Resizable property value is not inherited.
5.3.3Automatic Sizing
There are two kinds of automatic sizing in the DataGridView control: column fill mode and content-
based automatic sizing.
Column fill mode causes the visible columns in the control to fill the width of the control's display
area. For more information about this mode, see the Column Fill Mode section below.
You can also configure rows, columns, and headers to automatically adjust their sizes to fit their cell
contents. In this case, size adjustment occurs whenever cell contents change.
Note : If you maintain cell values in a custom data cache using virtual mode, automatic sizing
occurs when the user edits a cell value but does not occur when you alter a cached value
outside of a CellValuePushed event handler. In this case, call the UpdateCellValue method to
force the control to update the cell display and apply the current automatic sizing modes.
If content-based automatic sizing is enabled for one dimension only—that is, for rows but not columns,
or for columns but not rows—and WrapMode is also enabled, size adjustment also occurs whenever
the other dimension changes. For example, if rows but not columns are configured for automatic sizing
and WrapMode is enabled, users can drag column dividers to change the width of a column and row
heights will automatically adjust so that cell contents are still fully displayed.
If you configure both rows and columns for content-based automatic sizing and WrapMode is enabled,
the DataGridView control will adjust sizes whenever cell contents changed and will use an ideal cell
height-to-width ratio when calculating new sizes.
To configure the sizing mode for headers and rows and for columns that do not override the control
value, set one or more of the following DataGridView properties:
• ColumnHeadersHeightSizeMode
• RowHeadersWidthSizeMode
• AutoSizeColumnsMode
• AutoSizeRowsMode
Page 32 of 69
2477405.doc
To override the control's column sizing mode for an individual column, set its AutoSizeMode property
to a value other than NotSet. The sizing mode for a column is actually determined by its
InheritedAutoSizeMode property. The value of this property is based on the column's AutoSizeMode
property value unless that value is NotSet, in which case the control's AutoSizeColumnsMode value is
inherited.
Use content-based automatic resizing with caution when working with large amounts of data. To avoid
performance penalties, use the automatic sizing modes that calculate sizes based only on the displayed
rows rather than analyzing every row in the control. For maximum performance, use programmatic
resizing instead so that you can resize at specific times, such as immediately after new data is loaded.
Content-based automatic sizing modes do not affect rows, columns, or headers that you have hidden by
setting the row or column Visible property or the control RowHeadersVisible or
ColumnHeadersVisible properties to false. For example, if a column is hidden after it is automatically
sized to fit a large cell value, the hidden column will not change its size if the row containing the large
cell value is deleted. Automatic sizing does not occur when visibility changes, so changing the column
Visible property back to true will not force it to recalculate its size based on its current contents.
Programmatic content-based resizing affects rows, columns, and headers regardless of their visibility.
5.3.4Programmatic Resizing
When automatic sizing is disabled, you can programmatically set the exact width or height of rows,
columns, or headers through the following properties:
• RowHeadersWidth
• ColumnHeadersHeight
• DataGridViewRow.Height
• DataGridViewColumn.Width
You can also programmatically resize rows, columns, and headers to fit their contents using the
following methods:
• AutoResizeColumn
• AutoResizeColumns
• AutoResizeColumnHeadersHeight
• AutoResizeRow
• AutoResizeRows
• AutoResizeRowHeadersWidth
These methods will resize rows, columns, or headers once rather than configuring them for continuous
resizing. The new sizes are automatically calculated to display all cell contents without clipping. When
you programmatically resize columns that have InheritedAutoSizeMode property values of Fill,
however, the calculated content-based widths are used to proportionally adjust the column FillWeight
property values, and the actually column widths are then calculated according to these new
proportions so that all columns fill the available display area of the control.
Programmatic resizing is useful to avoid performance penalties with continuous resizing. It is also
useful to provide initial sizes for user-resizable rows, columns, and headers, and for column fill mode.
Page 33 of 69
2477405.doc
You will typically call the programmatic resizing methods at specific times. For example, you might
programmatically resize all columns immediately after loading data, or you might programmatically
resize a specific row after a particular cell value has been modified.
Sometimes you want your application to perform actions based on user selections within a
DataGridView control. Depending on the actions, you may want to restrict the kinds of selection that
are possible. For example, suppose your application can print a report for the currently selected record.
In this case, you may want to configure the DataGridView control so that clicking anywhere within a
row always selects the entire row, and so that only one row at a time can be selected.
You can specify the selections allowed by setting the SelectionMode property to one of the following
DataGridViewSelectionMode enumeration values.
DataGridViewSelecti
Description
onMode value
CellSelect Clicking a cell selects it. Row and column headers cannot be used for selection.
Clicking a cell selects it. Clicking a column header selects the entire column. Column
ColumnHeaderSelect
headers cannot be used for sorting.
Clicking a cell or a column header selects the entire column. Column headers cannot be
FullColumnSelect
used for sorting.
Page 34 of 69
2477405.doc
Default selection mode. Clicking a cell selects it. Clicking a row header selects the entire
RowHeaderSelect
row.
Note : Changing the selection mode at run time automatically clears the current selection.
By default, users can select multiple rows, columns, or cells by dragging with the mouse, pressing
CTRL or SHIFT while selecting to extend or modify a selection, or clicking the top-left header cell to
select all cells in the control. To prevent this behavior, set the MultiSelect property to false.
The FullRowSelect and RowHeaderSelect modes allow users to delete rows by selecting them and
pressing the DELETE key. Users can delete rows only when the current cell is not in edit mode, the
AllowUserToDeleteRows property is set to true, and the underlying data source supports user-driven
row deletion. Note that these settings do not prevent programmatic row deletion.
5.4.1Programmatic Selection
The current selection mode restricts the behavior of programmatic selection as well as user selection.
You can change the current selection programmatically by setting the Selected property of any cells,
rows, or columns present in the DataGridView control. You can also select all cells in the control
through the SelectAll method, depending on the selection mode. To clear the selection, use the
ClearSelection method.
If the MultiSelect property is set to true, you can add DataGridView elements to or remove them from
the selection by changing the Selected property of the element. Otherwise, setting the Selected
property to true for one element automatically removes other elements from the selection.
Note that changing the value of the CurrentCell property does not alter the current selection.
You can retrieve a collection of the currently selected cells, rows, or columns through the
SelectedCells, SelectedRows, and SelectedColumns properties of the DataGridView control. Accessing
these properties is inefficient when every cell in the control is selected. To avoid a performance
penalty in this case, use the AreAllCellsSelected method first. Additionally, accessing these collections
to determine the number of selected cells, rows, or columns can be inefficient. Instead, you should use
the GetCellCount, GetRowCount, or GetColumnCount method, passing in the Selected value.
5.5 Scrolling
The DataGridView obviously provides scrolling support via the horizontal and vertical scrollbars. It
also provides vertical scrolling via the mouse wheel. Scrolling horizontally scrolls in pixel values while
scrolling vertically scrolls in row index. The DataGridView does not provide support for scrolling rows
in pixel increments.
5.5.1Scroll event
As you scroll the DataGridView raises the Scroll event that allows you to be notified that scrolling is
occurring. The Orientation property on the scroll event args lets you know the scroll direction.
Page 35 of 69
2477405.doc
5.5.2Scroll bars
The DataGridView provides access to the scrollbars that it displays via the protected
HorizontalScrollBar and VerticalScrollBar properties. Accessing these ScrollBar controls directly allow
you to have finer control over scrolling.
5.5.3Scrolling Properties
There are a set of properties that provide greater level of details on how the DataGridView is scrolled.
The diagram highlights these properties and their values at this state. The properties are read/write
except for the FirstDisplayedScrollingColumnHiddenWidth and VerticalScrollingOffset properties.
5.6 Sorting
By default, users can sort the data in a DataGridView control by clicking the header of a text box
column. You can modify the SortMode property of specific columns to allow users to sort by other
column types when it makes sense to do so. You can also sort the data programmatically by any
column, or by multiple columns.
DataGridView columns have three sort modes. The sort mode for each column is specified through the
SortMode property of the column, which can be set to one of the following
DataGridViewColumnSortMode enumeration values.
DataGridViewColumnSortMode
Description
value
Default for text box columns. Unless column headers are used for selection,
Automatic clicking the column header automatically sorts the DataGridView by this
column and displays a glyph indicating the sort order.
NotSortable Default for non–text box columns. You can sort this column programmatically;
Page 36 of 69
2477405.doc
however, it is not intended for sorting, so no space is reserved for the sorting
glyph.
You can sort this column programmatically, and space is reserved for the
Programmatic
sorting glyph.
You might want to change the sort mode for a column that defaults to NotSortable if it contains values
that can be meaningfully ordered. For example, if you have a database column containing numbers that
represent item states, you can display these numbers as corresponding icons by binding an image
column to the database column. You can then change the numerical cell values into image display
values in a handler for the CellFormatting event. In this case, setting the SortMode property to
Automatic will enable your users to sort the column. Automatic sorting will enable your users to group
items that have the same state even if the states corresponding to the numbers do not have a natural
sequence. Check box columns are another example where automatic sorting is useful for grouping
items in the same state.
You can sort a DataGridView programmatically by the values in any column or in multiple columns,
regardless of the SortMode settings. Programmatic sorting is useful when you want to provide your
own user interface (UI) for sorting or when you want to implement custom sorting. Providing your
own sorting UI is useful, for example, when you set the DataGridView selection mode to enable
column header selection. In this case, although the column headers cannot be used for sorting, you still
want the headers to display the appropriate sorting glyph, so you would set the SortMode property to
Programmatic.
Columns set to programmatic sort mode do not automatically display a sorting glyph. For these
columns, you must display the glyph yourself by setting the
DataGridViewColumnHeaderCell.SortGlyphDirection property. This is necessary if you want
flexibility in custom sorting. For example, if you sort the DataGridView by multiple columns, you
might want to display multiple sorting glyphs or no sorting glyph.
Although you can programmatically sort a DataGridView by any column, some columns, such as
button columns, might not contain values that can be meaningfully ordered. For these columns, a
SortMode property setting of NotSortable indicates that it will never be used for sorting, so there is no
need to reserve space in the header for the sorting glyph.
When a DataGridView is sorted, you can determine both the sort column and the sort order by
checking the values of the SortedColumn and SortOrder properties. These values are not meaningful
after a custom sorting operation. For more information about custom sorting, see the Custom Sorting
section later in this topic.
When a DataGridView control containing both bound and unbound columns is sorted, the values in
the unbound columns cannot be maintained automatically. To maintain these values, you must
implement virtual mode by setting the VirtualMode property to true and handling the
CellValueNeeded and CellValuePushed events.
5.6.1Programmatic Sorting
You can sort a DataGridView programmatically by calling its Sort method.
Page 37 of 69
2477405.doc
Note : When the DataGridView control is bound to an external data source by setting the
DataSource property, the Sort(DataGridViewColumn,ListSortDirection) method overload does
not work for unbound columns. Additionally, when the VirtualMode property is true, you can
call this overload only for bound columns. To determine whether a column is data-bound,
check the IsDataBound property value. Sorting unbound columns in bound mode is not
supported.
5.6.2Custom Sorting
You can customize DataGridView by using the Sort(IComparer) overload of the Sort method or by
handling the SortCompare event.
The Sort(IComparer) method overload takes an instance of a class that implements the IComparer
interface as a parameter. This overload is useful when you want to provide custom sorting; for example,
when the values in a column do not have a natural sort order or when the natural sort order is
inappropriate. In this case, you cannot use automatic sorting, but you might still want your users to
sort by clicking the column headers. You can call this overload in a handler for the
ColumnHeaderMouseClick event if you do not use column headers for selection.
Note : The Sort(IComparer) method overload works only when the DataGridView control is
not bound to an external data source and the VirtualMode property value is false. To
customize sorting for columns bound to an external data source, you must use the sorting
operations provided by the data source. In virtual mode, you must provide your own sorting
operations for unbound columns.
To use the Sort(IComparer) method overload, you must create your own class that implements the
IComparer interface. This interface requires your class to implement the IComparer.Compare(Object)
method, to which the DataGridView passes DataGridViewRow objects as input when the
Sort(IComparer) method overload is called. With this, you can calculate the correct row ordering based
on the values in any column.
The Sort(IComparer) method overload does not set the SortedColumn and SortOrder properties, so you
must always set the DataGridViewColumnHeaderCell.SortGlyphDirection property to display the
sorting glyph.
As an alternative to the Sort(IComparer) method overload, you can provide custom sorting by
implementing a handler for the SortCompare event. This event occurs when users click the headers of
columns configured for automatic sorting or when you call the
Sort(DataGridViewColumn,ListSortDirection) overload of the Sort method. The event occurs for each
pair of rows in the control, enabling you to calculate their correct order.
Page 38 of 69
2477405.doc
Note : The SortCompare event does not occur when the DataSource property is set or when
the VirtualMode property value is true.
Note : The gridline color is used only with the Single, SingleHorizontal, and SingleVertical
values of the DataGridViewCellBorderStyle enumeration and the Single value of the
DataGridViewHeaderBorderStyle enumeration. The other values of these enumerations use
colors specified by the operating system. Additionally, when visual styles are enabled on
Windows XP and above, the GridColor property value is not used.
The following table identifies the standard border styles available via the :
BorderStyle value Description
None No border.
The following table identifies the advanced border styles available that can be set for the left, right, top
and bottom parts. Note that some combinations are not valid.
Page 39 of 69
2477405.doc
None No border.
In edit mode, the user can change the cell value and press ENTER to commit the change or ESC to
revert the cell to its original value.
You can configure a DataGridView control so that a cell enters edit mode as soon as it becomes the
current cell. The behavior of the ENTER and ESC keys is unchanged in this case, but the cell remains
in edit mode after the value is committed or reverted. You can also configure the control so that cells
enter edit mode only when users type in the cell or only when users press F2. Finally, you can prevent
cells from entering edit mode except when you call the BeginEdit method.
Editing begins when the cell receives focus. This mode is useful when pressing the
EditOnEnter TAB key to enter values across a row, or when pressing the ENTER key to enter
values down a column.
Editing begins when F2 is pressed while the cell has focus. This mode places the
EditOnF2
selection point at the end of the cell contents.
EditOnKeystroke Editing begins when any alphanumeric key is pressed while the cell has focus.
EditOnKeystrokeOrF2 Editing begins when any alphanumeric key or F2 is pressed while the cell has focus.
Page 40 of 69
2477405.doc
You can configure cell copying to copy cell values only, to include row and column header text in the
Clipboard data, or to include header text only when users select entire rows or columns.
The text values of selected cells can be copied to the Clipboard. Header
EnableAlwaysIncludeHeaderText
text is included for rows and columns that contain selected cells.
The text values of selected cells can be copied to the Clipboard. Row or
column header text is included for rows or columns that contain selected
EnableWithAutoHeaderText
cells only when the SelectionMode property is set to RowHeaderSelect or
ColumnHeaderSelect and at least one header is selected.
The text values of selected cells can be copied to the Clipboard. Header
EnableWithoutHeaderText
text is not included.
Depending on the selection mode, users can select multiple disconnected groups of cells. When a user
copies cells to the Clipboard, rows and columns with no selected cells are not copied. All other rows or
columns become rows and columns in the table of data copied to the Clipboard. Unselected cells in
these rows or columns are copied as blank placeholders to the Clipboard.
When users copy content, the DataGridView control adds a DataObject to the Clipboard. This data
object is retrieved from the GetClipboardContent() method. You can call this method when you want
to programmatically add the data object to the Clipboard. The GetClipboardContent() method retrieves
values for individual cells by calling the DataGridViewCell.GetClipboardContent() method. You can
override either or both of these methods in derived classes to customize the layout of copied cells or to
support additional data formats.
To achieve this behavior, you can freeze columns in the control. This is done via setting the Frozen
property on the column or row. When you freeze a column, all the columns to its left (or to its right in
right-to-left language scripts) are frozen as well. Frozen columns remain in place while all other
columns can scroll. Rows act in similar fashion: all rows before the frozen row are frozen as well and
remain in place while the non frozen rows can scroll.
Page 41 of 69
2477405.doc
5.11.1IDataGridViewEditingControl
Cells that support advanced editing functionality typically use a hosted control that is derived from a
Windows Forms control. This interface is implemented by editing controls, such as
DataGridViewComboBoxEditingControl and DataGridViewTextBoxEditingControl, that are hosted by
the corresponding DataGridView cells, such as DataGridViewComboBoxCell and
DataGridViewTextBoxCell, when they are in edit mode.
Cell types that can that host editing controls set their EditType property to a Type representing the
editing control type.
5.11.2IDataGridViewEditingCell
This interface is implemented by classes to provide a user interface (UI) for specifying values without
hosting an editing control. The UI in this case is displayed regardless of whether the cell is in edit
mode. The DataGridViewCheckBoxCell is an example of a cell that implements the
IDataGridViewEditingCell interface.
Other cell types, such as DataGridViewButtonCell, provide a UI but do not store user-specified values.
In this case, the cell type does not implement IDataGridViewEditingCell or host an editing control.
Page 42 of 69
2477405.doc
5.12.4Virtual-Mode Events
If your data is read-only, the CellValueNeeded event may be the only event you will need to handle.
Additional virtual-mode events let you enable specific functionality like user edits, row addition and
deletion, and row-level transactions.
Some standard DataGridView events (such as events that occur when users add or delete rows, or
when cell values are edited, parsed, validated, or formatted) are useful in virtual mode, as well. You can
also handle events that let you maintain values not typically stored in a bound data source, such as cell
ToolTip text, cell and row error text, cell and row shortcut menu data, and row height data.
The following events occur only when the VirtualMode property is set to true.
Event Description
Used by the control to retrieve a cell value from the data cache for display.
CellValueNeeded
This event occurs only for cells in unbound columns.
Used by the control to commit user input for a cell to the data cache. This
event occurs only for cells in unbound columns.
CellValuePushed Call the UpdateCellValue method when changing a cached value outside of a
CellValuePushed event handler to ensure that the current value is displayed
in the control and to apply any automatic sizing modes currently in effect.
NewRowNeeded Used by the control to indicate the need for a new row in the data cache.
CancelRowEdit Used by the control to indicate that a row should revert to its cached values.
The following events are useful in virtual mode, but can be used regardless of the VirtualMode
property setting.
Events Description
UserDeletingRow
UserDeletedRow Used by the control to indicate when rows are deleted or added, letting you
RowsRemoved update the data cache accordingly.
RowsAdded
Page 43 of 69
2477405.doc
CellFormatting
CellParsing
CellValidating Used by the control to format cell values for display and to parse and validate
CellValidated user input.
RowValidating
RowValidated
Used by the control to retrieve cell ToolTip text when the DataSource property
is set or the VirtualMode property is true.
CellToolTipTextNeeded
Cell ToolTips are displayed only when the ShowCellToolTips property value is
true.
Used by the control to retrieve cell or row error text when the DataSource
property is set or the VirtualMode property is true.
CellContextMenuStripNeeded Used by the control to retrieve a cell or row ContextMenuStrip when the control
RowContextMenuStripNeeded DataSource property is set or the VirtualMode property is true.
Used by the control to retrieve or store row height information in the data
RowHeightInfoNeeded cache. Call the UpdateRowHeightInfo method when changing the cached row
RowHeightInfoPushed height information outside of a RowHeightInfoPushed event handler to
ensure that the current value is used in the display of the control.
5.13 Capacity
In general, the DataGridView does not have any hard-coded capacity limits. The grid was designed so
that more and more content can be added as machines become faster and have more memory. That
said, the grid was not designed to deal with large number of columns. If you add more than 300
columns you will start to notice a degradation in performance as our performance tuning of the grid
was not designed for this. If you need a grid with large amounts of columns then the DataGridView
might not meet your needs. Regarding the number of rows supported, the DataGridView is bound by
memory constraints. When using Virtual mode you can easily support over 2 million rows. Check out
the best practices section below for information on things you can do (and not do) to improve memory
usage and performance.
Page 44 of 69
2477405.doc
6 Best Practices
The DataGridView control is designed to provide maximum scalability. If you need to display large
amounts of data, you should follow the guidelines described in this topic to avoid consuming large
amounts of memory or degrading the responsiveness of the user interface (UI).
• Avoid setting cell style properties for individual DataGridViewCell or DataGridViewRow objects.
This includes the row object specified by the RowTemplate property. Each new row that is cloned
from the row template will receive its own copy of the template's cell style object. For maximum
scalability, set cell style properties at the DataGridView level. For example, set the DefaultCellStyle
property rather than the DataGridViewCell.Style property.
• If some cells require formatting other than default formatting, use the same DataGridViewCellStyle
instance across groups of cells, rows, or columns. Avoid directly setting properties of type
DataGridViewCellStyle on individual cells, rows, and columns. For an example of cell style sharing,
see How to: Set Default Cell Styles for the Windows Forms DataGridView Control. You can also
avoid a performance penalty when setting cell styles individually by handling the CellFormatting
event handler. For an example, see How to: Customize Data Formatting in the Windows Forms
DataGridView Control.
• When determining a cell's style, use the DataGridViewCell.InheritedStyle property rather than the
DataGridViewCell.Style property. Accessing the Style property creates a new instance of the
DataGridViewCellStyle class if the property has not already been used. Additionally, this object
might not contain the complete style information for the cell if some styles are inherited from the
row, column, or control. For more information about cell style inheritance, see Cell Styles in the
Windows Forms DataGridView Control.
• Avoid creating shortcut menus for individual cells and rows. This includes the row template, which
is cloned along with its shortcut menu when new rows are added to the control. For maximum
scalability, use only the control's ContextMenuStrip property to specify a single shortcut menu for
the entire control.
• If you require multiple shortcut menus for multiple rows or cells, handle the
CellContextMenuStripNeeded or RowContextMenuStripNeeded events. These events let you
manage the shortcut menu objects yourself, allowing you to tune performance.
Page 45 of 69
2477405.doc
and headers. To determine the correct size, the DataGridView control must examine the value of each
cell that it must accommodate. When working with large data sets, this analysis can negatively impact
the performance of the control when automatic resizing occurs. To avoid performance penalties, use
the following guidelines:
• Avoid using automatic sizing on a DataGridView control with a large set of rows. If you do use
automatic sizing, only resize based on the displayed rows. Use only the displayed rows in virtual
mode as well.
• For rows and columns, use the DisplayedCells or DisplayedCellsExceptHeaders field of the
DataGridViewAutoSizeRowsMode, DataGridViewAutoSizeColumnsMode, and
DataGridViewAutoSizeColumnMode enumerations.
• For maximum scalability, turn off automatic sizing and use programmatic resizing.
6.4 Using the Selected Cells, Rows, and Columns Collections Efficiently
The SelectedCells collection does not perform efficiently with large selections. The SelectedRows and
SelectedColumns collections can also be inefficient, although to a lesser degree because there are many
fewer rows than cells in a typical DataGridView control, and many fewer columns than rows. To avoid
performance penalties when working with these collections, use the following guidelines:
• To determine whether all the cells in the DataGridView have been selected before you access the
contents of the SelectedCells collection, check the return value of the AreAllCellsSelected method.
Note, however, that this method can cause rows to become unshared. For more information, see
the next section.
• Avoid cell-based selection modes. Instead, set the SelectionMode property to FullRowSelect or
FullColumnSelect.
While sharing row instances saves memory, rows can easily become unshared. For example, whenever
a user interacts directly with a cell, its row becomes unshared. Because this cannot be avoided, the
guidelines in this topic are useful only when working with very large amounts of data and only when
users will interact with a relatively small part of the data each time your program is run.
A row cannot be shared in an unbound DataGridView control if any of its cells contain values. When
the DataGridView control is bound to an external data source or when you implement virtual mode
and provide your own data source, the cell values are stored outside the control rather than in cell
objects, allowing the rows to be shared.
Page 46 of 69
2477405.doc
A row object can only be shared if the state of all its cells can be determined from the state of the row
and the states of the columns containing the cells. If you change the state of a cell so that it can no
longer be deduced from the state of its row and column, the row cannot be shared.
In bound mode or virtual mode, you can provide ToolTips and shortcut menus for individual cells by
handling the CellToolTipTextNeeded and CellContextMenuStripNeeded events.
The DataGridView control will automatically attempt to use shared rows whenever rows are added to
the DataGridViewRowCollection. Use the following guidelines to ensure that rows are shared:
• Avoid calling the Add(Object[]) overload of the Add method and the Insert(Object[]) overload of the
Insert method of the Rows collection. These overloads automatically create unshared rows.
• Be sure that the row specified in the RowTemplate property can be shared in the following cases:
When calling the Add() or Add(Int) overloads of the Add method or the Insert(Int, Int)
overload of the Insert method of the Rows collection.
When increasing the value of the RowCount property.
When setting the DataSource property.
• Be sure that the row indicated by the indexSource parameter can be shared when calling the
AddCopy, AddCopies, InsertCopy, and InsertCopies methods of the Rows collection.
• Be sure that the specified row or rows can be shared when calling the Add(DataGridViewRow)
overload of the Add method, the AddRange method, the Insert(Int32,DataGridViewRow) overload
of the Insert method, and the InsertRange method of the Rows collection.
• Avoid indexing the Rows collection or iterating through it with a foreach loop. You will not typically
need to access rows directly. DataGridView methods that operate on rows take row index
arguments rather than row instances. Additionally, handlers for row-related events receive event
argument objects with row properties that you can use to manipulate rows without causing them to
become unshared.
Page 47 of 69
2477405.doc
through this method will modify all the rows that share this object. The row for new records is not
shared with other rows, however, so it will not be affected when you modify any other row. Note
also that different rows represented by a shared row may have different shortcut menus. To retrieve
the correct shortcut menu from a shared row instance, use the GetContextMenuStrip method and
pass in the row's actual index. If you access the shared row's ContextMenuStrip property instead, it
will use the shared row index of -1 and will not retrieve the correct shortcut menu.
• Avoid indexing the DataGridViewRow.Cells collection. Accessing a cell directly will cause its parent
row to become unshared, instantiating a new DataGridViewRow. Handlers for cell-related events
receive event argument objects with cell properties that you can use to manipulate cells without
causing rows to become unshared. You can also use the CurrentCellAddress property to retrieve
the row and column indexes of the current cell without accessing the cell directly.
• Avoid cell-based selection modes. These modes cause rows to become unshared. Instead, set the
SelectionMode property to DataGridViewSelectionMode.FullRowSelect or
DataGridViewSelectionMode.FullColumnSelect.
• Do not access the SelectedCells collection when the SelectionMode property value is
FullColumnSelect, ColumnHeaderSelect, FullRowSelect, or RowHeaderSelect. This causes all
selected rows to become unshared.
• Do not call the AreAllCellsSelected(boolean) method. This method can cause rows to become
unshared.
• Do not call the SelectAll method when the SelectionMode property value is CellSelect. This causes
all rows to become unshared.
• Do not set the ReadOnly or Selected property of a cell to false when the corresponding property in
its column is set to true. This causes all rows to become unshared.
• Do not access the DataGridViewRowCollection.List property. This causes all rows to become
unshared.
• Do not call the Sort(IComparer) overload of the Sort method. Sorting with a custom comparer
causes all rows to become unshared.
Page 48 of 69
2477405.doc
While you can navigate to a read-only cell, and you can set a read-only cell to be the current cell,
the content cannot be modified by the user. Note that the ReadOnly property does not keep the
cell from being modified programmatically. Also note that ReadOnly does not affect whether the
user can delete rows
Page 49 of 69
2477405.doc
public DataGridViewDisableButtonCell()
{
this.enabledValue = true;
}
Page 50 of 69
2477405.doc
The following example extends the DataGridViewTextColumn and cell to paint an image next to
the text. The sample uses the DataGridViewCellStyle.Padding property to adjust the text location
and overrides the Paint method to paint an icon. This sample can be simplified by handling the
CellPainting event and performing similar code.
public TextAndImageColumn()
{
this.CellTemplate = new TextAndImageCell();
}
Page 51 of 69
2477405.doc
c.imageSize = this.imageSize;
return c;
}
if (this.InheritedStyle != null) {
Padding inheritedPadding = this.InheritedStyle.Padding;
this.DefaultCellStyle.Padding = new Padding(imageSize.Width,
inheritedPadding.Top, inheritedPadding.Right,
inheritedPadding.Bottom);
}
}
}
}
private TextAndImageCell TextAndImageCellTemplate
{
get { return this.CellTemplate as TextAndImageCell; }
}
internal Size ImageSize
{
get { return imageSize; }
}
}
return imageValue;
}
else if (this.imageValue != null) {
return this.imageValue;
}
else {
return this.OwningTextAndImageColumn.Image;
}
}
set {
if (this.imageValue != value) {
this.imageValue = value;
Page 52 of 69
2477405.doc
this.imageSize = value.Size;
if (this.Image != null) {
// Draw the image clipped to the cell.
System.Drawing.Drawing2D.GraphicsContainer container =
graphics.BeginContainer();
graphics.SetClip(cellBounds);
graphics.DrawImageUnscaled(this.Image, cellBounds.Location);
graphics.EndContainer(container);
}
}
Page 53 of 69
2477405.doc
In the DataGridView control, the SortMode property value of a column determines its sorting
behavior.
Some datasources have built in support for sorting on multiple columns. If your datasource
implements IBindingListView and provides support for the Sort property, then using it will
provide support for multi-column sorting. To indicate in the grid that multiple columns are sorted
on, manually set a column’s SortGlyphDirection to properly indicate that the column is sorted.
The following example uses a DataTable and sets the default view’s Sort property to sort on the
second and third columns. The example also demonstrates setting the column’s
SortGlyphDirection. The example assumes that you have a DataGridView and a BindingSource
component on your form:
Page 54 of 69
2477405.doc
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
public Form1()
{
// Initialize the form.
// This code can be replaced with designer generated code.
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.SortCompare += new DataGridViewSortCompareEventHandler(
this.dataGridView1_SortCompare);
Controls.Add(this.dataGridView1);
this.Text = "DataGridView.SortCompare demo";
PopulateDataGridView();
}
Page 55 of 69
2477405.doc
using System;
using System.Drawing;
using System.Windows.Forms;
public Form1()
{
// Initialize the form.
// This code can be replaced with designer generated code.
AutoSize = true;
Text = "DataGridView IComparer sort demo";
FlowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
FlowLayoutPanel1.Location = new System.Drawing.Point(304, 0);
FlowLayoutPanel1.AutoSize = true;
Page 56 of 69
2477405.doc
FlowLayoutPanel1.Controls.Add(RadioButton1);
FlowLayoutPanel1.Controls.Add(RadioButton2);
FlowLayoutPanel1.Controls.Add(Button1);
Button1.Text = "Sort";
RadioButton1.Text = "Ascending";
RadioButton2.Text = "Descending";
RadioButton1.Checked = true;
Controls.Add(FlowLayoutPanel1);
Controls.Add(DataGridView1);
}
base.OnLoad(e);
}
Page 57 of 69
2477405.doc
// If the Last Names are equal, sort based on the First Name.
if (CompareResult == 0)
{
CompareResult = System.String.Compare(
DataGridViewRow1.Cells[0].Value.ToString(),
DataGridViewRow2.Cells[0].Value.ToString());
}
return CompareResult * sortOrderModifier;
}
}
}
NOTE: The DataGridView reuses editing controls across cells if the type is the same. Because of
this you should make sure that you do not continuously hook up a new event handler if there is
already one hooked up otherwise you your event handler will get called multiple times.
Page 58 of 69
2477405.doc
With the DataGridViewComboBox you can do the same thing by using the
DataGridView.EditingControlShowing event. The following code example demonstrates how to do
this. Note that the sample also demonstrates how to keep multiple SelectedIndexChanged events
from firing.
// Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = dataGridView1.DoDragDrop(
dataGridView1.Rows[rowIndexFromMouseDown],
DragDropEffects.Move);
}
}
}
Page 59 of 69
2477405.doc
if (rowIndexFromMouseDown != -1)
{
// Remember the point where the mouse down occurred.
// The DragSize indicates the size that the mouse can move
// before a drag event should be started.
Size dragSize = SystemInformation.DragSize;
// Create a rectangle using the DragSize, with the mouse position being
// at the center of the rectangle.
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
e.Y - (dragSize.Height / 2)),
dragSize);
}
else
// Reset the rectangle if the mouse is not over an item in the ListBox.
dragBoxFromMouseDown = Rectangle.Empty;
}
// If the drag operation was a move then remove and insert the row.
if (e.Effect== DragDropEffects.Move)
{
DataGridViewRow rowToMove = e.Data.GetData(
typeof(DataGridViewRow)) as DataGridViewRow;
dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
}
}
14.How do I make the last column wide enough to occupy all the
remaining client area of the grid?
By setting the AutoSizeMode for the last column to Fill the column will size itself to fill in the
remaining client area of the grid. Optionally you can set the last column’s MinimumWidth if you
want to keep the column from sizing too small.
Page 60 of 69
2477405.doc
The following code example uses the DataGridView.DefaultCellStyle property to set the wrap
mode for the entire control.
this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
this.dataGridViewImageColumn1.DefaultCellStyle.NullValue = null;
Page 61 of 69
2477405.doc
choose between a subcategory based upon the category. This is possible with the DataGridView by
using two combo box columns. To enable this, two versions of the filtered list (subcategory) needs
to be created. One list has no filter applied while the other one will be filtered only when the user
is editing a subcategory cell. Two lists are required due to the requirement described in 3.5.1
section that a combo box cells value must be in the items collection or else a DataError event is
raised. In this case, since all combo box cells in the column use the same datasource if you filter the
datasource for one row then a combo box cell in another row might not have its value visible in
the datasource, thus causing a DataError event.
The below example uses the Northwind database to display related data from the Territory and
Region tables (a territory is in a specific region.) Using the category and subcategory concept, the
Region is the category and the Territory is the subcategory.
this.filteredTerritoriesBS.RemoveFilter();
}
}
19.How do I show the error icon when the user is editing the
cell?
Page 62 of 69
2477405.doc
Sometimes when using the error text and icon feature you want an immediate feedback to the user
that something that they typed into a cell is incorrect. By default when setting the ErrorText
property the error icon will not appear if the cell is in edit mode such as a text box or combo box
cell.
The below sample demonstrates how you can set a cell’s padding in the CellValidating event to
provide spacing for the error icon. Since padding by default affects the location of the error icon
the sample uses the CellPainting to move the position of the icon for painting. Lastly, the sample
uses the tooltip control to display a custom tooltip when the mouse is over the cell to indicate
what the problem is. This sample could also be written as a custom cell that overrides
GetErrorIconBounds method to provide a location for the error icon that was independent of the
padding.
e.Cancel = true;
}
}
}
Page 63 of 69
2477405.doc
e.Handled = true;
}
}
if (cell.ErrorText != String.Empty)
{
if (!errorTooltip.Active)
{
errorTooltip.Show(cell.ErrorText, dataGridView1, 1000);
}
errorTooltip.Active = true;
}
}
}
Page 64 of 69
2477405.doc
The following code example demonstrates how to create an unbound column of check box cells to
enable the user to select database records to process. The grid is put into virtual mode and responds
to the necessary events. The selected records are kept by ID in a dictionary to allow the user to sort
the content but not lose the checked rows.
if (e.ColumnIndex == 0)
{
int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;
if (checkState.ContainsKey(orderID))
{
e.Value = checkState[orderID];
}
else
e.Value = false;
}
Page 65 of 69
2477405.doc
// Handle the notification that the value for a cell in the virtual column
// needs to be pushed back to the dictionary.
if (e.ColumnIndex == 0)
{
// Get the orderID from the OrderID column.
int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;
Implementing a master/detail form is easy using the interaction between the DataGridView
control and the BindingSource component. The below sample will show two related tables in the
Northwind SQL Server sample database: Customers and Orders. By selecting a customer in the
master DataGridView all the orders for the customer will appear in the detail DataGridView.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
[STAThreadAttribute()]
public static void Main()
{
Application.Run(new Form1());
}
Page 66 of 69
2477405.doc
detailsDataGridView.Dock = DockStyle.Fill;
this.Controls.Add(splitContainer1);
this.Load += new System.EventHandler(Form1_Load);
this.Text = "DataGridView master/detail demo";
}
// Resize the master DataGridView columns to fit the newly loaded data.
masterDataGridView.AutoResizeColumns();
// Create a DataSet.
DataSet data = new DataSet();
data.Locale = System.Globalization.CultureInfo.InvariantCulture;
Page 67 of 69
2477405.doc
masterBindingSource.DataSource = data;
masterBindingSource.DataMember = "Customers";
this.Validate();
this.customersBindingSource.EndEdit();
this.customersTableAdapter.Update(this.northwindDataSet.Customers);
if (!e.Row.IsNewRow)
{
DialogResult response = MessageBox.Show("Are you sure?", "Delete row?",
MessageBoxButtons.YesNo,
Page 68 of 69
2477405.doc
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (response == DialogResult.No)
e.Cancel = true;
}
Page 69 of 69