Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
110
Modify UltraGrid CellClickAction with Shift / Ctrl
posted

This seems like this question could be loosely related to this post:
http://forums.infragistics.com/forums/p/12017/45163.aspx#45163

I have an UltraGrid that has the following initial configuration:

    Grid.DisplayLayout.Override.SelectTypeCell = SelectType.SingleAutoDrag;
    Grid.DisplayLayout.Override.SelectTypeRow = SelectType.Extended;
    Grid.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect;

I need to be able to select cells initially so that the contents can be dragged off the grid.

However, I need to be able to hold down the shift/ctrl key and multiselect rows.

I've come closest with code that handles the keyDown and KeyUp events:
 
    protected override void grid_KeyDown(object sender, KeyEventArgs e)
    {
       // Turn on multiselect for Ctrl and Shift Keys
       if (e.Control || e.Shift)
       {
           lastSelectedRow.Selected = true;
           // "lastSelectedRow" is set in the grid_AfterSelectChanged event handler, below
           Grid.DisplayLayout.Override.CellClickAction = CellClickAction.RowSelect;
       }

       base.grid_KeyDown(sender, e);
    }

    protected override void grid_KeyUp(object sender, KeyEventArgs e)
    {
        // Turn off multiselect
        Grid.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect;
        base.grid_KeyUp(sender, e);
    }
   
    private void grid_AfterSelectChange(object sender, AfterSelectChangeEventArgs e)
    {
        if (grid.DisplayLayout.Override.CellClickAction == CellClickAction.RowSelect && grid.Selected.Rows.Count > 0)
            lastSelectedRow = grid.ActiveRow;
        else if (grid.DisplayLayout.Override.CellClickAction == CellClickAction.CellSelect && grid.Selected.Cells.Count > 0)
            lastSelectedRow = grid.ActiveCell.Row;
    }

What happens with this code is that Ctrl-clicking works exactly as expected, but a shift-click requires an extra click.
Here is what I'D LIKE to happen:

1) User selects a cell with left-click.
2) User presses and holds shift key.
3) User selects other row with left-click, still holding shift.

All rows from the cell/row selected in step 1 to the other row in step 3 should be selected.


Here is what DOES happen:

1) User selects a cell with left-click.
2) User presses and holds shift key.
3) User selects other row with left-click, still holding shift.

Cell selection in the row in step 1 is lost, and the row selected in step 3 is the only row selected (the whole row is selected, not just a cell)

4) Still holding shift, the user must click on yet another row.

All the rows from the row in step 3 to the one in step 4 are selected.

That additional click should not be required. Can anyone tell me how to ensure the UltraGrid "remembers" the row with the currently selected cell? Help would be much appreciated!

Incidentally, I have tried the following too, but I haven't got anywhere near as close to getting it to work as the above.

        private void Grid_BeforeSelectChange(object sender, BeforeSelectChangeEventArgs e)
        {
            bool control = (ModifierKeys & Keys.Control) == Keys.Control;
            bool shift = (ModifierKeys & Keys.Shift) == Keys.Shift;
            if (control || shift)
            {
                Grid.DisplayLayout.Override.CellClickAction = CellClickAction.RowSelect;
            }
            else if (Grid.DisplayLayout.Override.CellClickAction == CellClickAction.RowSelect)
            {
                Grid.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect;
            }
        }

  • 110
    posted

    Right, well found a solution:

            protected override void grid_KeyDown(object sender, KeyEventArgs e)
            {
                // Turn on multiselect for Ctrl and Shift Keys
                if ((e.Control || e.Shift) && lastSelectedRow != null)
                {
                    lastSelectedRow.Activate();
                    lastSelectedRow.Selected = true;
                    if (e.Shift)
                        Grid.PerformAction(UltraGridAction.ToggleRowSel, true, false);
                    Grid.DisplayLayout.Override.CellClickAction = CellClickAction.RowSelect;
                }

                base.grid_KeyDown(sender, e);
            }
     

    If you use that for the grid_KeyDown eventhandler, it works as expected. Not sure exactly why the ctrl was working while the shift wasn't.

    This seems like a *bit* of a hack - if there is a more elegant solution, please let me know. But, as I said, this works! :)