dotnetmonitor.com

 
Index
Previous
Next

 

The DataGridView control provides you with a variety of options for configuring how users can select cells, rows, and columns. For example, you can enable single or multiple selection, selection of whole rows or columns when users click cells, or selection of whole rows or columns only when users click their headers, which enables cell selection as well. If you want to provide your own user interface for selection, you can disable ordinary selection and handle all selection programmatically. Additionally, you can enable users to copy the selected values to the Clipboard.

 

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.

DataGridViewSelectionMode value

Description

CellSelect

Clicking a cell selects it. Row and column headers cannot be used for selection.

ColumnHeaderSelect

Clicking a cell selects it. Clicking a column header selects the entire column. Column headers cannot be used for sorting.

FullColumnSelect

Clicking a cell or a column header selects the entire column. Column headers cannot be used for sorting.

FullRowSelect

Clicking a cell or a row header selects the entire row.

RowHeaderSelect

Default selection mode. Clicking a cell selects it. Clicking a row header selects the entire 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.

What is the DataGridView
Differences between the DataGridView and DataGrid controls
Highlight of features
Structure of DGV
Architecture Elements
Cells and Bands