Multiple Cell Selection in DataGridView

9 Sep

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;
}
About these ads

3 Responses to “Multiple Cell Selection in DataGridView”

  1. Ari Block September 13, 2009 at 1:54 am #

    very cool thanks

  2. Rocks Tops December 29, 2009 at 1:16 am #

    Cool blog!

  3. Chris February 4, 2011 at 8:54 pm #

    Thanks just what I was searching for.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: