2016-07-22 55 views
1

不接收鼠标点击我尝试使用按钮文件名和路径加载到DataGridView的细胞。 我放在这样一个单元中的按钮:按钮时,放置在DataGridView的

var cellRectangle = dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false); 
int buttonX = dataGridView.Location.X + cellRectangle.Location.X + cellRectangle.Width - btnLoadFile.Width; 
int buttonY = dataGridView.Location.Y + cellRectangle.Location.Y; 

btnLoadFile.Location = new Point(buttonX + 20, buttonY); 
btnLoadFile.Height = cellRectangle.Height - 2; 
btnLoadFile.Visible = true; 
btnLoadFile.BringToFront(); 

我想用户点击该按钮,选择文件中的FileDialog等 问题是按钮不会获​​得任何点击。网格单元接收点击。

有没有人有一个想法,为什么它发生,我该如何处理呢?

+0

使按钮为其他数据网格视图的孩子重新路由的按钮单击事件 –

+0

@Uthistran:刚刚试了一下。该按钮仍然不响应点击.... – DmitryRibak

+0

这听起来不对。你可能只是没有把它连接起来。如果您可以看到并单击它,__ will_将调用其单击事件。当你在设计器中双击它时会发生什么? – TaW

回答

0

尝试......

dataGridView.Controls.SetChildIndex(btnLoadFile, 0); 
// Bring it to the front 
btnLoadFile.BringToFront(); 

也许你并不需要最后一行...

+0

不幸的是它并没有改变任何东西......同样的行为 – DmitryRibak

0

我会用DataGridViewButtonColumn而不是绘制网格上方的按钮。 这给你一个按钮内的每一行。

https://msdn.microsoft.com/de-de/library/system.windows.forms.datagridviewbuttoncolumn(v=vs.110).aspx

如果你想有一个显示电子邮件,例如另一个细胞内的按钮,就可以得到所需的editcell类型和实现文本框和按钮的组合,使按钮才可见在编辑模式时。看看这里 https://msdn.microsoft.com/de-de/library/7tas5c80(v=vs.100).aspx 获取更多信息。我会张贴你一些代码,但不幸的是,我只为自己的目的制作了自己的单元格/编辑控件。

+0

之前是的,我知道这个选项只是扔在表单上的按钮,将它放在一个细胞似乎要简单得多。另外我不喜欢整个柱子都装满了按钮,它不会让用户感到混乱 – DmitryRibak