zoukankan      html  css  js  c++  java
  • *****How to handle a doubleclick on a grid row or cell

    Solution

    from:

    http://www.devexpress.com/Support/Center/KB/p/A2934.aspx

    Basically, you should handle the GridView.DoubleClick, get the mouse cursor position, and pass it to the CalcHitInfo method to check whether the mouse points to a grid row, and not, for example, to column headers or footer. Finally, perform your action.

    [C#]

    using DevExpress.XtraGrid.Views.Grid;
    using DevExpress.XtraGrid.Views.Grid.ViewInfo;

    private void gridView1_DoubleClick(object sender, EventArgs e) {
        GridView view = (GridView)sender;
        Point pt = view.GridControl.PointToClient(Control.MousePosition);
        DoRowDoubleClick(view, pt);
    }

    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));
        }
    }

    [VB.NET]

    Imports DevExpress.XtraGrid.Views.Grid
    Imports DevExpress.XtraGrid.Views.Grid.ViewInfo

    Private Sub gridView1_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles
    gridView1.DoubleClick
        Dim view As GridView = CType(sender, GridView)
        Dim pt As Point = view.GridControl.PointToClient(Control.MousePosition)
        DoRowDoubleClick(view, pt)
    End Sub

    Private Shared Sub DoRowDoubleClick(ByVal view As GridView, ByVal pt As Point)
        Dim info As GridHitInfo = view.CalcHitInfo(pt)
        If info.InRow OrElse info.InRowCell Then
            Dim colCaption As String
            If info.Column Is Nothing Then
                colCaption = "N/A"
            Else
                colCaption = info.Column.GetCaption()
            End If
            MessageBox.Show(String.Format("DoubleClick on row: {0}, column: {1}.", info.RowHandle,
    colCaption))
        End If
    End Sub

    The above code works fine until your grid is not editable, said differently, until the GridView.OptionsBehavior.Editable option is set to False. Once you enable inplace editing, a click on a grid cell activates an editor. After that all mouse events are sent to that very editor and cannot be handled at the GridView level, so your GridView.DoubleClick event handler is no longer being executed.

    If you need inplace editing and also need to handle double-clicks, set the GridView.OptionsBehavior.EditorShowMode property to Click. This is the easiest solution. In this case, the DoubleClick event is fired before an inplace editor is activated, so your code is executed as expected.

    However, end-users may complain that now, with option EditorShowMode set to Click, they have to click twice to activate a cell editor. These complains may force you to revert EditorShowMode to its Default value and improve your double-click handler.

    As it was mentioned above, the grid doesn't receive mouse events while a cell editor is activate. This means that the double-click event must be handled at the editor level.

    To attach/detach event handlers of an inplace editor, use the ShownEditor and HiddenEditor events, as explained in the How to assign an event handler to the in-place editor article.

    [C#]

    using DevExpress.XtraEditors;
    using DevExpress.XtraGrid;

    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);
    }

    [VB.NET]

    Imports DevExpress.XtraEditors
    Imports DevExpress.XtraGrid

    Private inplaceEditor As BaseEdit
    Private Sub gridView1_ShownEditor(ByVal sender As Object, ByVal e As EventArgs) Handles
    gridView1.ShownEditor
        inplaceEditor = (CType(sender, GridView)).ActiveEditor
        AddHandler inplaceEditor.DoubleClick, AddressOf inplaceEditor_DoubleClick
    End Sub

    Private Sub gridView1_HiddenEditor(ByVal sender As Object, ByVal e As EventArgs) Handles
    gridView1.HiddenEditor
        If inplaceEditor IsNot Nothing Then
            RemoveHandler inplaceEditor.DoubleClick, AddressOf inplaceEditor_DoubleClick
            inplaceEditor = Nothing
        End If
    End Sub

    Private Sub inplaceEditor_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim editor As BaseEdit = CType(sender, BaseEdit)
        Dim grid As GridControl = CType(editor.Parent, GridControl)
        Dim pt As Point = grid.PointToClient(Control.MousePosition)
        Dim view As GridView = CType(grid.FocusedView, GridView)
        DoRowDoubleClick(view, pt)
    End Sub

    Besides handling the inplace editor's DoubleClick event, you still need to handle the grid's DoubleClick to allow a user to double-click the row indicator.

    See Also:
    How to assign an event handler to the in-place editor
    How to Disable the Immediate Opening of In-Place Editors
    Why is the DoubleClick event for the GridView not triggered?

    ===============================

    This example demonstrates three different solutions of handling the double-click event on a grid row or column cell. The solution you should choose for your project depends on your grid settings: whether it allows editing and how an inplace editor is activated. Please refer to the How to handle a double-click on a grid row or cell article to learn implementation details.

    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);
            }
        }
    }

  • 相关阅读:
    xlrd doc
    安装Python package
    Python处理Excel(转载)
    OPENSSL简介
    sublime text2教程
    使用SQL 从表中取记录
    SQL基础
    shell脚本之grep的使用方法
    (转载)(收藏)OceanBase深度解析
    (转载)线程池的使用
  • 原文地址:https://www.cnblogs.com/luoyaoquan/p/2136264.html
Copyright © 2011-2022 走看看