KB Article A306
Visible to All Users
Duplicate

We have closed this ticket because another page addresses its subject:

Grid - Provide the capability to select multiple rows via mouse without Ctrl or Shift

How to Select Rows via the Mouse

Description:
How to Select Rows via the Mouse

Answer:
On occassion, you may find it necessary to provide users with a simple way to select rows via the mouse - press the mouse button and move it up or down - just like a standard ListBox selects rows in MultiExtended selection mode. The XtraGrid does not have this feature built-in, however, it is easy to implement via the Mouse~ events of the GridView class. You will find the sample code below. Generally, you may want to implement this feature in a GridView descendant class, if you need it in multiple grids.

C#
private void SelectRows(GridView view, int startRow, int endRow) { if(startRow > -1 && endRow > -1) { view.BeginSelection(); view.ClearSelection(); view.SelectRange(startRow, endRow); view.EndSelection(); } } private int GetRowAt(GridView view, int x, int y) { return view.CalcHitInfo(new Point(x, y)).RowHandle; } private int StartRowHandle = -1; private int CurrentRowHandle = -1; private void gridView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { StartRowHandle = GetRowAt(sender as GridView, e.X, e.Y); } private void gridView1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { int newRowHandle = GetRowAt(sender as GridView, e.X, e.Y); if(CurrentRowHandle != newRowHandle) { CurrentRowHandle = newRowHandle; SelectRows(sender as GridView, StartRowHandle, CurrentRowHandle); } } private void gridView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { StartRowHandle = -1; CurrentRowHandle = -1; }

See also:
How to obtain the text of selected rows and copy it to the clipboard
How to implement block selection for XtraGrid cells
How to implement a Copy/Paste feature

Comments (2)

    Is there a reason for not including this functionality into the XtraGrid suite? Could you please create a feature request for that?
    Also, this workaround does not support automatic scrolling when the mouse cursor gets to the top or the bottom of the grid during the selection.
    See also http://www.devexpress.com/Support/Center/Question/Details/Q97055, http://www.devexpress.com/Support/Center/Example/Details/E636

    Andrew Ser (DevExpress Support) 11 years ago

      Hello,
      By default, you can select rows only by the mouse by moving it over the row indicator panel.
       However, I agree that selection via hovering over cells with the mouse will be good improvement for our grid.
      I've passed this task to our R&D team - Grid - Provide the capability to select multiple rows via mouse without Ctrl or Shift. Probably, we will implement it in the future. We will update that ticket as soon as we make progress.

      I've modified the suggested solution so that it scrolls the grid automatically when you select rows. Please test the attachment.

      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.