在 System.Windows.Forms.DataGrid 中双击单元格时不会触发 MouseDoubleClick 事件
MouseDoubleClick event is not fired when a cell is double-clicked in System.Windows.Forms.DataGrid
我需要处理在 ReadOnly DataGrid(不幸的是不是 DataGridView)中双击单元格,但没有触发 MouseDoubleClick 事件。我怎样才能让活动火起来?
我正在创建 DataGrid 并订阅事件:
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 文本框,它似乎吃掉了第二个点击事件:
我发现 an old thread in Experts Exchange,他们说的一样多:
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 控件,所以如果有办法抑制 "activating" 中的单元格或显示它,那对我来说也是一个足够好的解决方案。
注意:我知道 DataGrid 已过时,但我正在处理遗留代码,请不要评论告诉我使用 DataGridView - 它对我没有帮助。
当单元格上发生鼠标按下时,TextBox
编辑控件获得焦点并接收其他鼠标上下移动,因此DataGrid的双击事件不会引发。
由于您的 DataGrid
是只读的,您可以将 DataGridTextBoxColumn 更改为不显示编辑控件。这样双击事件将引发。为此,覆盖 Edit
方法的重载并且什么都不做就足够了:
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!");
}
我需要处理在 ReadOnly DataGrid(不幸的是不是 DataGridView)中双击单元格,但没有触发 MouseDoubleClick 事件。我怎样才能让活动火起来?
我正在创建 DataGrid 并订阅事件:
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 文本框,它似乎吃掉了第二个点击事件:
我发现 an old thread in Experts Exchange,他们说的一样多:
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 控件,所以如果有办法抑制 "activating" 中的单元格或显示它,那对我来说也是一个足够好的解决方案。
注意:我知道 DataGrid 已过时,但我正在处理遗留代码,请不要评论告诉我使用 DataGridView - 它对我没有帮助。
当单元格上发生鼠标按下时,TextBox
编辑控件获得焦点并接收其他鼠标上下移动,因此DataGrid的双击事件不会引发。
由于您的 DataGrid
是只读的,您可以将 DataGridTextBoxColumn 更改为不显示编辑控件。这样双击事件将引发。为此,覆盖 Edit
方法的重载并且什么都不做就足够了:
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!");
}