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
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
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.