Back to Devexpress

How to: Handle a Double-Click on a Grid Row or Cell

windowsforms-403568-controls-and-libraries-data-grid-examples-navigation-and-selection-how-to-handle-a-double-click-on-a-grid-row-or-cell.md

latest6.3 KB
Original Source

How to: Handle a Double-Click on a Grid Row or Cell

  • Nov 23, 2021
  • 3 minutes to read

The BaseView.Editable property specifies whether the Data Grid is editable. Use different techniques depending on this setting.

If Data Grid Is NOT Editable

Handle the BaseView.DoubleClick event. You can use View HitInfo objects to determine which Grid element was clicked.

csharp
private void gridView_DoubleClick(object sender, EventArgs e) {
    DXMouseEventArgs ea = e as DXMouseEventArgs;
    GridView view = sender as GridView;
    GridHitInfo info = view.CalcHitInfo(ea.Location);
    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));
    }
}
vb
Private Sub gridView_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles gridView.DoubleClick
    Dim ea As DXMouseEventArgs = TryCast(e, DXMouseEventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    Dim info As GridHitInfo = view.CalcHitInfo(ea.Location)
    If info.InRow OrElse info.InRowCell Then
        Dim colCaption As String = If(info.Column Is Nothing, "N/A", info.Column.GetCaption())
        MessageBox.Show(String.Format("DoubleClick on row: {0}, column: {1}.", info.RowHandle, colCaption))
    End If
End Sub

If Data Grid Is Editable

If the Data Grid is editable, a double-click on a cell invokes an in-place cell editor. Editors intercept mouse events and the BaseView.DoubleClick event is never fired. You can set the View.OptionsBehavior.EditorShowMode property to Click to force the View.DoubleClick event to fire before an in-place editor is activated. In this case you can handle the DoubleClick event as shown in the previous section.

If you do not wish to change the EditorShowMode property, handle the DoubleClick event at the editor level. To do so, handle the ColumnView.ShownEditor and ColumnView.HiddenEditor events.

csharp
BaseEdit editor;
private void gridView_ShownEditor(object sender, EventArgs e) {
    GridView view = sender as GridView;
    editor = view.ActiveEditor;
    editor.DoubleClick += editor_DoubleClick;
}

void gridView_HiddenEditor(object sender, EventArgs e) {
    editor.DoubleClick -= editor_DoubleClick;
    editor = null;
}

void editor_DoubleClick(object sender, EventArgs e) {
    BaseEdit editor = (BaseEdit)sender;
    GridControl grid = editor.Parent as GridControl;
    GridView view = grid.FocusedView as GridView;
    Point pt = grid.PointToClient(Control.MousePosition);
    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));
    }
}
vb
Private editor As BaseEdit
Private Sub gridView_ShownEditor(ByVal sender As Object, ByVal e As EventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    editor = view.ActiveEditor
    AddHandler editor.DoubleClick, AddressOf editor_DoubleClick
End Sub

Private Sub gridView_HiddenEditor(ByVal sender As Object, ByVal e As EventArgs)
    RemoveHandler editor.DoubleClick, AddressOf editor_DoubleClick
            editor = Nothing
End Sub

Private Sub editor_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
    Dim editor As BaseEdit = DirectCast(sender, BaseEdit)
    Dim grid As GridControl = TryCast(editor.Parent, GridControl)
    Dim view As GridView = TryCast(grid.FocusedView, GridView)
    Dim pt As Point = grid.PointToClient(Control.MousePosition)
    Dim info As GridHitInfo = view.CalcHitInfo(pt)
    If info.InRow OrElse info.InRowCell Then
        Dim colCaption As String = If(info.Column Is Nothing, "N/A", info.Column.GetCaption())
        MessageBox.Show(String.Format("DoubleClick on row: {0}, column: {1}.", info.RowHandle, colCaption))
    End If
End Sub

Double-Click Row Indicator

To handle Row Indicator Panel double clicks, handle the BaseView.DoubleClick event as demonstrated in the first section.

csharp
private void gridView1_DoubleClick(object sender, EventArgs e) {
    DXMouseEventArgs ea = e as DXMouseEventArgs;
    GridView view = sender as GridView;
    GridHitInfo info = view.CalcHitInfo(ea.Location);
    if (info.HitTest == GridHitTest.RowIndicator) {
        MessageBox.Show(string.Format("DoubleClick on row indicator, row #{0}", info.RowHandle));
    }
}
vb
Private Sub gridView1_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
    Dim ea As DXMouseEventArgs = TryCast(e, DXMouseEventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    Dim info As GridHitInfo = view.CalcHitInfo(ea.Location)
    If info.HitTest = GridHitTest.RowIndicator Then
        MessageBox.Show(String.Format("DoubleClick on row indicator, row #{0}", info.RowHandle))
    End If
End Sub

See Also