Ticket T1284656
Visible to All Users

Re: MultiRow select not working with Shift/Ctrl + click?

created 10 days ago

Dear Support,

further to the ticket https://supportcenter.devexpress.com/ticket/details/t900302, here is an alternative implementation, that allows a bit more flexibility!

With this code, you can do the usual 2 steps range selection:

  • 1 select the start row without pressing Shift
  • 2 select the end row pressing Shift and ending selecting the range
Code
private int _startRowHandle; private void GridControl_OnPreviewMouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton != MouseButton.Left) return; TableView view = (TableView)sender; GridControl grid = (GridControl)view.Parent; TableViewHitInfo hitTest; if (Keyboard.Modifiers != ModifierKeys.Shift) { hitTest = view.CalcHitInfo(e.GetPosition(view)); if (hitTest.InRow) { _startRowHandle = hitTest.RowHandle; return; } } if (Keyboard.Modifiers != ModifierKeys.Shift) return; hitTest = view.CalcHitInfo(e.GetPosition(view)); if (hitTest.InRow) grid.SelectRange(_startRowHandle, hitTest.RowHandle); }

Answers approved by DevExpress Support

created 9 days ago

Hello Stephan,

Thank you for sharing your solution and making it public.

As an additional alternative, it is possible to handle the PreviewMouseDown event at the parent container level and utilize GridControl's CurrentItem instead of SelectedItem. In this case, the operation will be processed before CurrentItem is updated.

XAML
<Grid PreviewMouseDown="UIElement_OnPreviewMouseDown"> <dxg:GridControl x:Name="grid" ItemsSource="{Binding Source}" SelectionMode="MultipleRow" >
C#
private void UIElement_OnPreviewMouseDown(object sender, MouseButtonEventArgs e) { if (Keyboard.Modifiers == ModifierKeys.Shift && e.ChangedButton == MouseButton.Left) { var hitTest = view.CalcHitInfo(e.GetPosition(view)); if (hitTest.InRow) { if (grid.CurrentItem == null) grid.SelectItem(hitTest.RowHandle); grid.SelectRange(grid.GetRowHandleByListIndex(((IList)grid.ItemsSource).IndexOf(grid.CurrentItem)), hitTest.RowHandle); } } }

While this implementation may require further modification, it behaves in a similar manner.

Regards,
Alexander

    Comments (2)
    SE SE
    Stephan EHRET 4 days ago

      Dear Support,

      Thank you, this sounds promising.

      I’d like however to mention, that your sample works only partially: when selecting the last line with shift, the range covers the first row up to the select last selected row minus one row

      • See the small screen recording attached

      Viele Grüße / Best regards / Bien cordialement,
      Stephan Ehret

      Marc T (DevExpress Support) 3 days ago

        Hello Stephan,

        Thank you for the update. I indeed see the issue. Please accept our apologies.

        My tests show that it occurs because GridControl toggles the selection state of the clicked row after calling SelectRange. I also see that the issue does not occur if I use Dispatcher.BeginInvoke to call SelectRange with a slight delay.

        C#
        private void UIElement_OnPreviewMouseDown(object sender, MouseButtonEventArgs e) { if (Keyboard.Modifiers == ModifierKeys.Shift && e.ChangedButton == MouseButton.Left) { var hitTest = view.CalcHitInfo(e.GetPosition(view)); if (hitTest.InRow) { if (grid.CurrentItem == null) grid.SelectItem(hitTest.RowHandle); int currentRowHandle = grid.GetRowHandleByListIndex(((IList)grid.ItemsSource).IndexOf(grid.CurrentItem)); int targetRowHandle = hitTest.RowHandle; Dispatcher.BeginInvoke(() => { grid.SelectRange(currentRowHandle, targetRowHandle); }); } } }

        Please try similar changes in your application and let me know if this helps.

        Regards,
        Marc

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.