标题:怎么在datagridview的一行上添加右键菜单???
只看楼主
wzh19890404
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2009-9-28
结帖率:50%
已结贴  问题点数:16 回复次数:6 
怎么在datagridview的一行上添加右键菜单???
比如datagridview有一列属性性别 第一行是男 第二行是女
我在第一行上点击鼠标右键会弹出菜单A!!在第二行上右键就出现菜单B
就是根据该行上某个属性来确定弹出的是哪个菜单
搜索更多相关主题的帖子: 菜单 datagridview 
2009-10-18 21:17
wzh19890404
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2009-9-28
得分:0 
为什么没人来啊
2009-10-18 21:25
边境
Rank: 1
等 级:新手上路
帖 子:5
专家分:7
注 册:2009-10-18
得分:5 
从vs的工具箱托一个ContextMentStrip控件,
2009-10-18 21:38
wzh19890404
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2009-9-28
得分:0 
回复 3楼 边境
那不还是在控件上添加吗?那样不就一点控件随便哪个区域就出菜单了么??我是新手 希望具体点
2009-10-18 21:59
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
得分:5 
        
程序代码:
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) 
        { 
            if (e.Button == MouseButtons.Right) 
            { 
                if (this.dataGridView1.SelectedCells[0].Value != null) 
                { 
                    if (this.dataGridView1.SelectedCells[0].Value.ToString() == "男") 
                    { 
                        ContextMenuStrip contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(); 
                        ToolStripMenuItem a1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
                        ToolStripMenuItem a2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
                        ToolStripMenuItem a3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
                        a1ToolStripMenuItem.Name = "a1ToolStripMenuItem"; 
                        a1ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); 
                        a1ToolStripMenuItem.Text = "A1"; 
 
                        a2ToolStripMenuItem.Name = "a2ToolStripMenuItem"; 
                        a2ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); 
                        a2ToolStripMenuItem.Text = "A2"; 
 
                        a3ToolStripMenuItem.Name = "a3ToolStripMenuItem"; 
                        a3ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); 
                        a3ToolStripMenuItem.Text = "A3"; 
 
                        contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 
                        a1ToolStripMenuItem, 
                        a2ToolStripMenuItem, 
                        a3ToolStripMenuItem}); 
                        contextMenuStrip1.Name = "contextMenuStrip1"; 
                        contextMenuStrip1.Size = new System.Drawing.Size(153, 92); 
 
                        this.dataGridView1.SelectedCells[0].ContextMenuStrip = contextMenuStrip1; 
 
                    } 
                    else if (this.dataGridView1.SelectedCells[0].Value.ToString() == "女") 
                    { 
                        ContextMenuStrip contextMenuStrip2 = new System.Windows.Forms.ContextMenuStrip(); 
                        ToolStripMenuItem b1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
                        ToolStripMenuItem b2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
                        ToolStripMenuItem b3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 
                        b1ToolStripMenuItem.Name = "a1ToolStripMenuItem"; 
                        b1ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); 
                        b1ToolStripMenuItem.Text = "B1"; 
 
                        b2ToolStripMenuItem.Name = "a2ToolStripMenuItem"; 
                        b2ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); 
                        b2ToolStripMenuItem.Text = "B2"; 
 
                        b3ToolStripMenuItem.Name = "a3ToolStripMenuItem"; 
                        b3ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); 
                        b3ToolStripMenuItem.Text = "B3"; 
 
                        contextMenuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 
                        b1ToolStripMenuItem, 
                        b2ToolStripMenuItem, 
                        b3ToolStripMenuItem}); 
                        contextMenuStrip2.Name = "contextMenuStrip1"; 
                        contextMenuStrip2.Size = new System.Drawing.Size(153, 92); 
 
                        this.dataGridView1.SelectedCells[0].ContextMenuStrip = contextMenuStrip2; 
                    } 
                } 
  
            } 
        }

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-10-18 22:04
wzh19890404
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2009-9-28
得分:0 
回复 5楼 jedypjd
这个行吗?好像不行啊……
2009-10-19 11:02
YOUDIANSHUI
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:108
注 册:2008-12-10
得分:5 
感觉5楼的可行,试下就是了
但我觉得要改下.SelectedCells[0].变为 dataGridView1 .SelectedColumns【0】
2009-10-19 13:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-289059-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.529307 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved