Wednesday, April 13, 2011

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?

From stackoverflow
  • 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);
    }
    
  • How would you unselect something if it is accidentally selected?

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.