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:
- CustomGrid.cs (VB: CustomGrid.vb)
- Form1.cs (VB: Form1.vb)
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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;
}
}
}
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;
}
}
}