Example T495772
Visible to All Users

WinForms Data Grid - Select multiple rows by dragging the mouse in RowSelect mode

In GridMultiSelectMode.CellSelect mode, users can select a contiguous block of cells by dragging the mouse over them. To select multiple rows in the same way in GridMultiSelectMode.RowSelect mode, create a GridView descendant and override GridHandler.OnMouseDown and GridHandler.OnMouseMove methods:

C#
protected override bool OnMouseDown(MouseEventArgs ev) { var result = base.OnMouseDown(ev); if (ev.Button == MouseButtons.Left && Control.ModifierKeys == Keys.None && View.IsRowSelect && DownPointHitInfo.InRowCell) mouseDownHitInfo = DownPointHitInfo; else mouseDownHitInfo = null; return result; } protected override bool OnMouseMove(MouseEventArgs ev) { var result = base.OnMouseMove(ev); if (!result && ev.Button == MouseButtons.Left && mouseDownHitInfo != null) { var dragSize = SystemInformation.DragSize; var dragRect = new Rectangle( new Point(mouseDownHitInfo.HitPoint.X - dragSize.Width / 2, mouseDownHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize); if (!dragRect.Contains(ev.Location)) { mouseDownHitInfo = null; View.StartAccessSelection(); result = true; } } return result; }

Files to look at:

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

DXApplication3/CustomGrid.cs(vb)
C#
using DevExpress.Utils; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Registrator; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Base.Handler; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace DXApplication3 { [ToolboxItem(true)] public class CustomGrid : GridControl { protected override BaseView CreateDefaultView() { return CreateView("CustomGridView"); } protected override void RegisterAvailableViewsCore(InfoCollection collection) { base.RegisterAvailableViewsCore(collection); collection.Add(new CustomGridViewInfoRegistrator()); } } public class CustomGridViewInfoRegistrator : GridInfoRegistrator { public override string ViewName { get { return "CustomGridView"; } } public override BaseView CreateView(GridControl grid) { return new CustomGridView(grid); } public override BaseViewHandler CreateHandler(BaseView view) { return new CustomGridViewHandler(view as CustomGridView); } } public class CustomGridView : GridView { public virtual bool IsRowSelect { get { return IsMultiSelect && OptionsSelection.MultiSelectMode == GridMultiSelectMode.RowSelect; } } public CustomGridView() { } public CustomGridView(GridControl grid) : base(grid) { } protected override string ViewName { get { return "CustomGridView"; } } internal new void StartAccessSelection() { base.StartAccessSelection(); } public override EditorShowMode GetShowEditorMode() { if (OptionsBehavior.EditorShowMode == EditorShowMode.Default && IsRowSelect) return EditorShowMode.Click; return base.GetShowEditorMode(); } } public class CustomGridViewHandler : DevExpress.XtraGrid.Views.Grid.Handler.GridHandler { private GridHitInfo mouseDownHitInfo; public new CustomGridView View { get { return base.View as CustomGridView; } } public CustomGridViewHandler(GridView view) : base(view) { } protected override bool OnMouseDown(MouseEventArgs ev) { var result = base.OnMouseDown(ev); if (ev.Button == MouseButtons.Left && Control.ModifierKeys == Keys.None && View.IsRowSelect && DownPointHitInfo.InRowCell) mouseDownHitInfo = DownPointHitInfo; else mouseDownHitInfo = null; return result; } protected override bool OnMouseMove(MouseEventArgs ev) { var result = base.OnMouseMove(ev); if (!result && ev.Button == MouseButtons.Left && mouseDownHitInfo != null) { var dragSize = SystemInformation.DragSize; var dragRect = new Rectangle( new Point(mouseDownHitInfo.HitPoint.X - dragSize.Width / 2, mouseDownHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize); if (!dragRect.Contains(ev.Location)) { mouseDownHitInfo = null; View.StartAccessSelection(); result = true; } } return result; } } }
DXApplication3/Form1.cs(vb)
C#
using System; using System.Data; namespace DXApplication3 { public partial class Form1 : DevExpress.XtraEditors.XtraForm { public Form1() { InitializeComponent(); customGrid1.DataSource = CreateTable(42); customGridView1.OptionsSelection.MultiSelect = true; } private DataTable CreateTable(int RowCount) { DataTable tbl = new DataTable(); tbl.Columns.Add("Name", typeof(string)); tbl.Columns.Add("ID", typeof(int)); tbl.Columns.Add("Number", typeof(int)); tbl.Columns.Add("Date", typeof(DateTime)); for (int i = 0; i < RowCount; i++) tbl.Rows.Add(new object[] { String.Format("Name{0}", i), i, i % 4, DateTime.Now.AddDays(i) }); return tbl; } } }

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.