Share via


Selection Modes in the Windows Forms DataGridView Control

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 System.Windows.Forms.DataGridView.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.

NoteNote

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.

Programmatic 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.

See Also

Tasks

How to: Set the Selection Mode of the Windows Forms DataGridView Control

Reference

DataGridView
MultiSelect
SelectionMode
DataGridViewSelectionMode

Other Resources

Selection and Clipboard Use with the Windows Forms DataGridView Control