Вы можете выбрать ячейки самостоятельно, немного изменив цветовую схему сетки, и сохранить / удалить их в / из списка при нажатии на них:
public partial class Form1 : Form
{
/// <summary>
/// Currently selected cells.
/// </summary>
private List<DataGridViewCell> _selectedCells = new List<DataGridViewCell>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
/* Just for test
dataGridView1.Columns.Add("A", "ColA");
dataGridView1.Columns.Add("B", "ColB");
dataGridView1.Columns.Add("C", "ColC");
dataGridView1.Columns.Add("D", "ColD");
dataGridView1.Rows.Add("A1","B1","C1","D1");
dataGridView1.Rows.Add("A2", "B2", "C2", "D2");
dataGridView1.Rows.Add("A3", "B3", "C3", "D3");
dataGridView1.Rows.Add("A4", "B4", "C4", "D4");
dataGridView1.Rows.Add("A5", "B5", "C5", "D5");
dataGridView1.Rows.Add("A6", "B6", "C6", "D6");
*/
dataGridView1.MultiSelect = false;
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// Edited and added this line to avoid problems when clicking on the header.
if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
// Clears all the selected cells.
dataGridView1.ClearSelection();
if (_selectedCells.Contains(dataGridView1[e.ColumnIndex, e.RowIndex]))
{
_selectedCells.Remove(dataGridView1[e.ColumnIndex, e.RowIndex]);
dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = dataGridView1.DefaultCellStyle.BackColor;
}
else
{
_selectedCells.Add(dataGridView1[e.ColumnIndex, e.RowIndex]);
dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = dataGridView1.DefaultCellStyle.SelectionBackColor;
}
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
dataGridView1.ClearSelection();
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
dataGridView1.BeginEdit(true);
}
}