C# 实现dataGridView选中一行右键出现菜单的示例代码
作者:孙正嗯大正 时间:2022-05-12 05:15:55
在窗体中添加DataGridView控件和ConTextMenuStrip1控件,修改DataGridView属性,将contextMenuStrip控件绑定dataGridView控件
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //选中整行可在属性中修改
datagridview.AutoGenerateColumns = false;//不让datagridview自动生成列,可在属性中修改
datagridview.AllowUserToAddRows = true;//禁止自动生成行可在属性中修改
代码实现选中一行,右键出现菜单
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
dataGridView1.ClearSelection();
dataGridView1.Rows[e.RowIndex].Selected = true;
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
}
catch (Exception)
{
MessageBox.Show("请选择内容");
}
来源:https://blog.csdn.net/sunzheng176/article/details/108302385
标签:C#,dataGridView,选中一行,右键,菜单
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你如何用好 Java 中的枚举
2022-11-03 10:31:21
SpringBoot security安全认证登录的实现方法
2021-05-30 08:09:56
![](https://img.aspxhome.com/file/2023/5/63685_0s.png)
spring cloud升级到spring boot 2.x/Finchley.RELEASE遇到的坑
2022-01-04 20:40:56
![](https://img.aspxhome.com/file/2023/6/90026_0s.jpg)
java实现多个文件压缩成压缩包
2022-12-06 17:45:54
如何用Java注解和反射实现依赖注入
2022-11-04 11:34:10
java抓取网页或文件中的邮箱号码
2023-07-30 19:19:28
![](https://img.aspxhome.com/file/2023/9/62119_0s.jpg)
C#使用Redis的基本操作
2023-12-03 11:03:51
![](https://img.aspxhome.com/file/2023/7/79127_0s.png)
SpringCloud2020整合Nacos-Bootstrap配置不生效的解决
2023-11-29 02:42:47
Java结构型设计模式中建造者模式示例详解
2023-04-24 13:37:31
![](https://img.aspxhome.com/file/2023/6/60286_0s.png)
SpringBoot2.0集成MQTT消息推送功能实现
2022-02-28 01:55:04
![](https://img.aspxhome.com/file/2023/3/94233_0s.png)
微信公众平台开发教程(三) 基础框架搭建
2022-08-12 00:33:50
![](https://img.aspxhome.com/file/2023/5/84485_0s.png)
c#使用listbox的详细方法和常见问题解决
2023-08-27 11:28:56
![](https://img.aspxhome.com/file/2023/9/68069_0s.jpg)
java设计模式之简单工厂模式简述
2021-06-14 17:11:20
![](https://img.aspxhome.com/file/2023/8/79158_0s.jpg)
springcloud-gateway集成knife4j的示例详解
2023-11-29 08:56:44
![](https://img.aspxhome.com/file/2023/9/60789_0s.png)
基于集合的子集与集合的全排列的相关问题
2023-09-23 07:03:46
Unity使用多态制作计算器功能
2022-10-15 02:50:06
![](https://img.aspxhome.com/file/2023/4/91354_0s.png)
浅析C#的复制和克隆
2022-08-30 15:27:13
Java之NIO基本简介
2021-12-20 22:15:44
![](https://img.aspxhome.com/file/2023/5/82335_0s.png)
一个依赖搞定 Spring Boot 接口防盗刷的流程分析
2023-06-01 16:46:41
![](https://img.aspxhome.com/file/2023/4/93344_0s.png)
SpringBoot 监控管理模块actuator没有权限的问题解决方法
2022-01-26 21:50:11