C# - DatagridView 和 ContextMenuStrip
我有一个包含五列的 datagridview 和包含项目和子项目的上下文菜单条。当我右键单击最后一列时,我想打开上下文菜单。
我尝试了这段代码,但它打开了上下文菜单条,没有子项目。
dataGrid.Columns[dataGrid.Columns.Count].HeaderCell.ContextMenuStrip = contextMenuStrip1;
I have a datagridview with five columns and context menu strip which have items and sub items. When I right click on last column I want to open context menu.
I tried this code, but it's open context menu strip without sub items.
dataGrid.Columns[dataGrid.Columns.Count].HeaderCell.ContextMenuStrip = contextMenuStrip1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的用户右键单击 DataGridView 最后一列的标题,您似乎想要打开 ContextMenuStrip。我将使用 DataGridView
MouseDown
事件,并在该事件中检查这些条件,如果满足,则调用 ContextMenuStrip 的Show
方法。像这样:
It looks like you want to open your ContextMenuStrip if your user right clicks the header of your DataGridView's last column. I would use the DataGridView
MouseDown
event and in that event check for these conditions and if they're met call theShow
method of your ContextMenuStrip.Like this:
如果您的意思是要将上下文菜单附加到最后一列的标题,那么您的方向可能是正确的。但最后一列的索引是
dataGrid.Columns.Count - 1
。所以,这段代码对我来说效果很好:子项已就位。
If you mean you want to attach context menu to header of your last column, your direction is probably right. But index of last column is
dataGrid.Columns.Count - 1
. So, this code works fine for me:Subitems are in place.