Example E595
Visible to All Users

WinForms Data Grid - How to handle a double-click on a data row or cell

The Grid Control does not fire the DoubleClick event when a user double-clicks within an active cell editor. This example shows how to intercept mouse double-clicks within cell editors (enabled or disabled) (to respond to double-click mouse events within the active cell editor, rows, and cells).

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

DoubleClickCell/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; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.XtraEditors; using DevExpress.XtraGrid; namespace DoubleClickCell { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { new DevExpress.XtraGrid.Design.XViewsPrinting(gridControl1); radioGroup1_SelectedIndexChanged(radioGroup1, EventArgs.Empty); } private static void DoRowDoubleClick(GridView view, Point pt) { GridHitInfo info = view.CalcHitInfo(pt); if(info.InRow || info.InRowCell) { string colCaption = info.Column == null ? "N/A" : info.Column.GetCaption(); MessageBox.Show(string.Format("DoubleClick on row: {0}, column: {1}.", info.RowHandle, colCaption)); } } private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e) { gridView1.DoubleClick -= gridView1_DoubleClick; gridView1.ShownEditor -= gridView1_ShownEditor; gridView1.HiddenEditor -= gridView1_HiddenEditor; switch(radioGroup1.SelectedIndex) { case 0: // Requires a single event handler. Works always. gridView1.OptionsBehavior.Editable = false; gridView1.DoubleClick += gridView1_DoubleClick; break; case 1: // Requires a single event handler. Doesn't work when double-clicking an open inplace editor. gridView1.OptionsBehavior.Editable = true; gridView1.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click; gridView1.DoubleClick += gridView1_DoubleClick; break; case 2: // Requires 3 event handlers. Works always. gridView1.OptionsBehavior.Editable = true; gridView1.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Default; gridView1.DoubleClick += gridView1_DoubleClick; gridView1.ShownEditor += gridView1_ShownEditor; gridView1.HiddenEditor += gridView1_HiddenEditor; break; } } private void gridView1_DoubleClick(object sender, EventArgs e) { GridView view = (GridView)sender; Point pt = view.GridControl.PointToClient(Control.MousePosition); DoRowDoubleClick(view, pt); } BaseEdit inplaceEditor; private void gridView1_ShownEditor(object sender, EventArgs e) { inplaceEditor = ((GridView)sender).ActiveEditor; inplaceEditor.DoubleClick += inplaceEditor_DoubleClick; } private void gridView1_HiddenEditor(object sender, EventArgs e) { if(inplaceEditor != null) { inplaceEditor.DoubleClick -= inplaceEditor_DoubleClick; inplaceEditor = null; } } void inplaceEditor_DoubleClick(object sender, EventArgs e) { BaseEdit editor = (BaseEdit)sender; GridControl grid = (GridControl)editor.Parent; Point pt = grid.PointToClient(Control.MousePosition); GridView view = (GridView)grid.FocusedView; DoRowDoubleClick(view, pt); } } }

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.