如何让contextMenuStrip有条件的显示

kelei12399 2010-05-15 04:33:26
winform里,我想让contextMenuStrip在datagridview里在数据行里显示右键功能。,无数据的行不出现右键的快捷方式

如何实现,多谢
...全文
141 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
捷哥1999 2010-05-15
  • 打赏
  • 举报
回复
首先不要给DataGridView设置ContextMenuStrip,然后这样:

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex != -1 && dataGridView1.Rows[e.RowIndex].Cells[0] != null)
{
Point p = dataGridView1.PointToClient(Cursor.Position);
this.contextMenuStrip1.Show(dataGridView1, p);
}
}
}
zzx509 2010-05-15
  • 打赏
  • 举报
回复

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
Point p=dataGridView1.PointToClient(MousePosition);
System.Windows.Forms.DataGridView.HitTestInfo hitInfo=dataGridView1.HitTest(p.X, p.Y);
if (hitInfo.RowIndex < 0 || hitInfo.ColumnIndex < 0||(dataGridView1[hitInfo.ColumnIndex, hitInfo.RowIndex].Value == null ))
{
e.Cancel = true;
}

}
mngzilin 2010-05-15
  • 打赏
  • 举报
回复
MouseDwon事件

if(e.Button==鼠标右键&&datagridview.SelectedRows.Count>0)
{
//显示
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧