在System.Windows.Forms.DataGrid中双击单元格时,不会触发MouseDoubleClick事件 [英] MouseDoubleClick event is not fired when a cell is double-clicked in System.Windows.Forms.DataGrid

查看:100
本文介绍了在System.Windows.Forms.DataGrid中双击单元格时,不会触发MouseDoubleClick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理在ReadOnly DataGrid(不幸的是不是DataGridView)中双击一个单元格,但是没有触发MouseDoubleClick事件的情况。我该如何触发事件?

I need to handle when a cell is double-clicked in a ReadOnly DataGrid (not DataGridView unfortunately), but the MouseDoubleClick event is not being fired. How can I make the event fire?

我正在创建DataGrid并订阅该事件:

I'm creating the DataGrid and subscribing to the event:

var table = new DataTable();
table.Columns.Add("foo");
table.Rows.Add(new object[] { "foo" });
table.Rows.Add(new object[] { "foo" });
dataGrid1.DataSource = table;
dataGrid1.MouseDoubleClick += DataGrid1_MouseDoubleClick;
dataGrid1.ReadOnly = true;

仅当我双击单元格之间的边界时,才发生此事件。当我单击一个单元格时,出现一个ReadOnly文本框,似乎在吞噬第二个单击事件:

The event is only if I double click on the border between cells. When I click on a cell a ReadOnly textbox appears which seems to eat the second click event:

我发现专家交流中的老话题,他们在其中说了很多话:

I found an old thread in Experts Exchange, where they say as much:


嗯,不仅是双击单击单元格时未捕获到事件,则datagrid消息队列未捕获到该事件。我继承了一个数据网格并覆盖了wndproc,检查是否可以检测到双击。它捕获单击消息,但没有WM_LBUTTONDBLCLK消息通过。我怀疑MS具有子单元控件(请参见DataGridColumnStyle类和派生类)钩住了网格控件,并阻止了消息甚至继续传递到网格。试图预先勾住那个孩子或网格可能会产生非常混乱的结果,所以我避免这样做。

Well, not only is the double click event not being captured when clicking on a cell, it is not being caught by the datagrid message queue. I inherited a datagrid and overrode the wndproc, checking to see if I could detect the double click. It captures the click message, but no WM_LBUTTONDBLCLK message comes through. I suspect that MS has the child cell control (see DataGridColumnStyle class and derivatives) hook the grid control and prevent the message from even continuing to the grid. Trying to pre-hook that child or the grid could have pretty messy results, so I am avoiding that.

我不是真的需要TextBox控件,因此,如果有一种抑制单元格激活或显示它的方法,那对我来说也是一个很好的解决方案。

I don't really need the TextBox control so if there is a way to suppress the cells from "activating" or showing it, that would be a good enough solution for me as well.

注意:我知道DataGrid已过时,但是我正在处理旧代码,请不要发表评论告诉我使用DataGridView,这对我没有帮助。

推荐答案

在单元格上按下鼠标时, TextBox 编辑控件将获得焦点并接收其他鼠标起伏不定,因此 DataGrid 的双击事件不会引发。

When a mouse down occurs on a cell, the TextBox editing control gets the focus and receives other mouse ups and downs, so double click event of DataGrid will not raise.

由于您的 DataGrid 是只读的,您可以将 DataGridTextBoxColumn 更改为不显示编辑控件。这样,双击事件将引发。为此,足以覆盖此 Edit 方法的重载并且不执行任何操作:

Since your DataGrid is read-only, you can change DataGridTextBoxColumn to not show editing control. This way double click event will raise. To do so, it's enough to override this overload of Edit method and do nothing:

public class MyDataGridTextBoxColumn : DataGridTextBoxColumn
{
    protected override void Edit(CurrencyManager source, int rowNum,
        Rectangle bounds, bool readOnly, string displayText, bool cellIsVisible)
    {
    }
}

示例

private void Form1_Load(object sender, EventArgs e)
{
    var dt = new DataTable();
    dt.Columns.Add("A");
    dt.Columns.Add("B");
    dt.Rows.Add("1", "11");
    dt.Rows.Add("2", "22");
    var dg = new DataGrid();
    dg.Dock = DockStyle.Fill;
    this.Controls.Add(dg);
    dg.BringToFront();
    dg.DataSource = dt;
    var ts = new DataGridTableStyle();
    ts.GridColumnStyles.Add(new MyDataGridTextBoxColumn() { MappingName = "A" });
    ts.GridColumnStyles.Add(new MyDataGridTextBoxColumn() { MappingName = "B" });
    dg.TableStyles.Add(ts);
    dg.DoubleClick += dg_DoubleClick;
}
void dg_DoubleClick(object sender, EventArgs e)
{
    MessageBox.Show("DoubleClick!");
}

这篇关于在System.Windows.Forms.DataGrid中双击单元格时,不会触发MouseDoubleClick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆