C# DataGridView 项双击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/763162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:14:20  来源:igfitidea点击:

DataGridView item double click

c#.netwinformsdatagridview

提问by

I have a DataGridView in a Windows Form. I want to handle double click events on each cell to display a detail form related to that record. Unfortunately, the double click event is executed when you double click on column headers. What should I do?

我在 Windows 窗体中有一个 DataGridView。我想处理每个单元格上的双击事件以显示与该记录相关的详细信息表单。不幸的是,双击列标题时会执行双击事件。我该怎么办?

采纳答案by Mehrdad Afshari

You should check the RowIndexand ColumnIndexproperty of the event arguments. If one of them is negative, it means that either a row header or a column header is clicked, you should ignore that event.

您应该检查事件参数的RowIndexandColumnIndex属性。如果其中一个为负,则表示单击了行标题或列标题,您应该忽略该事件。

回答by Cerebrus

What event are you handling? Ideally you should handle the CellDoubleClickor the CellMouseDoubleClickevents and opening your details form in that handler. The latter event will catch a Double click using any of the mouse buttons (the button can be queried using the MouseEventArgs.Buttonproperty.

你在处理什么事件?理想情况下,您应该处理CellDoubleClickCellMouseDoubleClick事件并在该处理程序中打开您的详细信息表单。后一个事件将使用任何鼠标按钮捕获双击(可以使用MouseEventArgs.Button属性查询按钮。