Example E636
Visible to All Users

WinForms Data Grid - Select rows with the mouse

This example handles MouseDown, MouseMove, and MouseUp events to allow users to select data rows by moving the mouse pointer across grid rows while the mouse button is pressed.

Files to Review

Documentation

Does this example address your development requirements/objectives?

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

Example Code

WindowsApplication1/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { new DevExpress.XtraGrid.Design.XViewsPrinting(gridControl1); gridView1.OptionsSelection.MultiSelect = true; gridView1.OptionsBehavior.Editable = false; gridView1.OptionsSelection.EnableAppearanceFocusedCell = false; } 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; } } }

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.