private void ChangeCellColorToolStripMenuItem_Click(object sender, EventArgs e) { ColorDialog colorDialog = new ColorDialog(); DataGridViewSelectedCellCollection cellCollection = dataGridView1.SelectedCells; IEnumerable<DataGridViewCell> cellList = cellCollection.Cast<DataGridViewCell>(); List<DataGridViewCell> dgvcList = cellList.ToList(); List<Cell> undoList = new List<Cell>(); // create list of cells that are being changed for (int j = 0; j < dgvcList.Count; j++) { int column = dgvcList[j].ColumnIndex; int row = dgvcList[j].RowIndex; undoList.Add(sheet.GetCell(column, row)); } // push list to undo stack sheet.AddtoUndoStack(undoList); if (colorDialog.ShowDialog() == DialogResult.OK) { Color newColor = colorDialog.Color; // change color of selected cells based on dialog input for (int i = 0; i < dgvcList.Count; i++) { DataGridViewCell cell = dgvcList[i]; ChangeCellColor(cell, newColor); } } }