Custom an event for DataGridView.ContextMenuStrip

本文介绍如何为 DataGridView 的 ContextMenuStrip 属性定制一个类似 RowContextMenuStripNeeded 和 CellContextMenuStripNeeded 事件的解决方案。主要通过重写 WndProc 方法捕获鼠标右键点击和双击消息,计算当前鼠标位置的列索引和行索引,触发自定义的 ContextMenuStripNeeded 事件。
摘要由CSDN通过智能技术生成

download source code and demo(C# 2005) -- 25.3KB

Introduction


DataGridView's RowContextMenuStripNeeded event is useful to display a shortcut menu determined by a row's current state or the values it contains, its basic usage steps are:

  • Design a ContextMenuStrip menu control, then assign it to DataGridView.RowTemplate.ContextMenuStrip.
  • Capture DataGridView's RowContextMenuStripNeeded event,obtain the RowIndex and ContextMenuStrip object, and then realize self-defined event handler.


However, RowContextMenuStripNeeded events occurs only when the DataGridView DataSource is set or its VirtualMode is true.(reference MSDN: DataGridView.RowContextMenuStripNeeded Event) The RowContextMenuStripNeeded event does not occur too if DataGridView has no rows/columns or the right click outside valid rows such as ColumnHeaders/RowHeaders, empty area, etc..The another event CellContextMenuStripNeeded has the similar limitations and shortcomings.

This article introduces a solution for custuming an event for DataGridView.ContextMenuStrip property, which has the features like xxxContextMenuStripNeeded events. The main i

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值