Multiple Cell Selection in DataGridView

September 9, 2009

DataGridView has a facility to select multiple cells (or a range of cells) using mouse dragging, Cltrl+Click or Shift+Click. This feature is similar to cell selection in spreadsheet applications such as Microsoft Excel.

In order to enable this feature in DataGridView, use ‘SelectionMode‘ property as follows:

DataGridViewCell.SelectionMode = DataGridViewSelectionMode.CellSelect;

To read/write data from/to selected cells simply write:

foreach (DataGridViewCell cell in dataGridView1.SelectedCells)

{

//read cell data

MessageBox.Show(cell.Value.ToString());

//change cell data

dataGridView1.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Value = 1;

}