How can I get the value of all the selected cells in a DataGridView?

Y_Y picture Y_Y · Dec 30, 2009 · Viewed 17.2k times · Source

I have a DataGridView that has MultiSelect = true. After the user selects different cells from different rows how can I get the value of all the selected cells?

Answer

Mark Byers picture Mark Byers · Dec 30, 2009

You can iterate over SelectedCells.

foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
    MessageBox.Show(cell.Value.ToString());
}

You asked only for the value, but you probably also want to know the row and the column of the cell otherwise the value could be meaningless. You can access these also on the cell object.