Cornel
Cornel

Reputation:

Disbale DataGridViewRow unselect when CTRL is pressed

If the CTRL key is pressed and you click a selected DataGridViewRow the row is unselected. How can I stop this?

Upvotes: 1

Views: 334

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062780

That is the standard behaviour for multi-selection using Ctrl. Why would you break the user's expected interface? You could possibly hack around it by detecting selection changes (I'll look...)

(edit) - yes, seems to work if you hook SelectionChanged, something like:

DataGridViewRow[] lastSelectedRows = new DataGridViewRow[0];
void grid_SelectionChanged(object sender, System.EventArgs e) {
    if ((Control.ModifierKeys & Keys.Control) == Keys.Control) {
        foreach (DataGridViewRow row in lastSelectedRows) {
            if (!row.Selected) row.Selected = true;
        }            
    }
    DataGridViewSelectedRowCollection selected = grid.SelectedRows;
    lastSelectedRows = new DataGridViewRow[selected.Count];
    selected.CopyTo(lastSelectedRows, 0);
}

Upvotes: 1

Related Questions