I don't have a much simpler solution, but this should work. The whole idea is to change the selected BackColor lines to SelectionBackColor and the selected ForeColor lines to SelectionForeColor . They look as if they are selected. I suppose DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect it is easy to get SelectedRows , and not select rows by clicking on row headers. I also assume that Form.KeyPreview = true
Here is my code:
//This is to copy the SelectedRows every time user releases the Control key DataGridViewRow[] selectedRows; private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e){ if(selectedRows == null) return; if(!selectedRows.Contains(dataGridView.Rows[e.RowIndex])){ //Restore the BackColor and ForeColor of the selected rows foreach(DataGridViewRow row in selectedRows){ row.DefaultCellStyle.BackColor = dataGridView.DefaultCellStyle.BackColor; row.DefaultCellStyle.ForeColor = dataGridView.DefaultCellStyle.ForeColor; } } } private void form_KeyUp(object sender, KeyEventArgs e){ if(e.KeyCode == Keys.ControlKey){ selectedRows = new DataGridViewRow[dataGridView.SelectedRows.Count]; dataGridView.SelectedRows.CopyTo(selectedRows,0); foreach(DataGridViewRow row in selectedRows){ row.DefaultCellStyle.BackColor = dataGridView.DefaultCellStyle.SelectionBackColor; row.DefaultCellStyle.ForeColor = dataGridView.DefaultCellStyle.SelectionForeColor; } } }
I hope you understand the whole idea, to set it up and encode it yourself, for example, restore BackColor and ForeColor if the lost focus is dataGridView ...
I hope this helps, and someone can give a better solution!
source share