gpt4 book ai didi

c# - datagridview contextmenustrip : showing only for chosen rows?

转载 作者:行者123 更新时间:2023-11-30 14:34:33 25 4
gpt4 key购买 nike

我有标准的 datagridview 和 contextmenustrip。我的问题是当用户单击鼠标右键而不是每一行时,我需要显示此上下文菜单条!仅在我选择的行上。我试过这个:

dataGridView1.Rows[1].ContextMenuStrip = contextMenuStrip1;

但它不起作用。

最佳答案

在我看来,如果您的用户右键单击满足某些条件的 DataGridView 列的标题,您想打开 ContextMenuStrip。

简而言之:使用 DataGridView MouseDown 事件并在该事件中检查条件,如果满足则调用 Show 方法你的 ContextMenuStrip。

您可以引用的代码示例:

private void dataGridView1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
var ht = dataGridView1.HitTest(e.X, e.Y);
// Place your condition HERE !!!
// Currently it allow right click on last column only
if (( ht.ColumnIndex == dataGridView1.Columns.Count - 1)
&& (ht.Type == DataGridViewHitTestType.ColumnHeader)) {
// This positions the menu at the mouse's location
contextMenuStrip1.Show(MousePosition);
}
}
}

关于c# - datagridview contextmenustrip : showing only for chosen rows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13902211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com