DataGridView 的右键菜单(ContextMenuStrip)
2017-12-04 10:22
567 查看
一、使用属性
DataGridView, DataGridViewColumn, DataGridViewRow, DataGridViewCell 有 ContextMenuStrip 属性。可以通过设定 ContextMenuStrip 对象来控制 DataGridView 的右键菜单的显示。 DataGridViewColumn 的 ContextMenuStrip 属性设定了 除了列头以外的单元格的右键菜单。 DataGridViewRow 的 ContextMenuStrip 属性设定了除了行头以外的单元格的右键菜单。DataGridViewCell
的 ContextMenuStrip 属性设定了指定单元格的右键菜单。
[C#]
对于单元格上的右键菜单的设定,优先顺序是: Cell > Row > Column > DataGridView
二、使用dataGridView的 CellContextMenuStripNeeded、RowContextMenuStripNeeded 事件
利用 CellContextMenuStripNeeded 事件可以设定单元格的右键菜单,尤其但需要右键菜单根据单元格值的变化而变化的时候(例如你需要判断cell的值大于10时使用ContextMenuStrip1,小于10时使用ContextMenuStrip2 ,那就需要用CellContextMenuStripNeeded事件来设置了)。比起使用循环遍历,使用该事件来设定右键菜单的效率更高。但是,在DataGridView使用了DataSource绑定而且是VirtualMode的时候,该事件将不被引发。
[C#]
同样,可以通过 RowContextMenuStripNeeded 事件来设定行的右键菜单。
[C#]
CellContextMenuStripNeeded 事件处理方法的参数中「e.ColumnIndex=-1」表示行头、「e.RowIndex=-1」表示列头。RowContextMenuStripNeeded则不存在「e.RowIndex=-1」的情况。
————————————————————分割线——————————————————————————————
1、上面只是设置ContextMenuStrip的方法,右键并不会改变当前的行号,也就是说:当前行号是1,但右键单击第2行,弹出ContextMenuStrip菜单,这时当前行依然是第1行,currentRow取得的数据也是第1行的数据,那么我们怎么使右键的同时改变当前行号呢?
可以使用CellMouseDown事件解决这一问题:
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
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];
//contextMenuStrip_ListViewItemRightClick.Show(MousePosition.X, MousePosition.Y);
}
}
}
2、如果我们不需要根据单元格的值来区别设置不同的ContextMenuStrip,那么使用RowTemplate的ContextMenuStrip属性设置ContextMenuStrip就足够了。
——————————————————再分割——————————————————————————————
对于treeview也有类似的问题,解决方法:
//对于treeview可以应用mousedown事务
办法一:
办法二:
DataGridView, DataGridViewColumn, DataGridViewRow, DataGridViewCell 有 ContextMenuStrip 属性。可以通过设定 ContextMenuStrip 对象来控制 DataGridView 的右键菜单的显示。 DataGridViewColumn 的 ContextMenuStrip 属性设定了 除了列头以外的单元格的右键菜单。 DataGridViewRow 的 ContextMenuStrip 属性设定了除了行头以外的单元格的右键菜单。DataGridViewCell
的 ContextMenuStrip 属性设定了指定单元格的右键菜单。
[C#]
二、使用dataGridView的 CellContextMenuStripNeeded、RowContextMenuStripNeeded 事件
利用 CellContextMenuStripNeeded 事件可以设定单元格的右键菜单,尤其但需要右键菜单根据单元格值的变化而变化的时候(例如你需要判断cell的值大于10时使用ContextMenuStrip1,小于10时使用ContextMenuStrip2 ,那就需要用CellContextMenuStripNeeded事件来设置了)。比起使用循环遍历,使用该事件来设定右键菜单的效率更高。但是,在DataGridView使用了DataSource绑定而且是VirtualMode的时候,该事件将不被引发。
[C#]
[C#]
————————————————————分割线——————————————————————————————
1、上面只是设置ContextMenuStrip的方法,右键并不会改变当前的行号,也就是说:当前行号是1,但右键单击第2行,弹出ContextMenuStrip菜单,这时当前行依然是第1行,currentRow取得的数据也是第1行的数据,那么我们怎么使右键的同时改变当前行号呢?
可以使用CellMouseDown事件解决这一问题:
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
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];
//contextMenuStrip_ListViewItemRightClick.Show(MousePosition.X, MousePosition.Y);
}
}
}
2、如果我们不需要根据单元格的值来区别设置不同的ContextMenuStrip,那么使用RowTemplate的ContextMenuStrip属性设置ContextMenuStrip就足够了。
——————————————————再分割——————————————————————————————
对于treeview也有类似的问题,解决方法:
//对于treeview可以应用mousedown事务
办法一:
相关文章推荐
- DataGridView 的右键菜单(ContextMenuStrip)
- DataGridView 的右键菜单(ContextMenuStrip)
- DataGridView 的右键菜单(ContextMenuStrip)
- [转]DataGridView 的右键菜单(ContextMenuStrip)
- [转]DataGridView 的右键菜单(ContextMenuStrip)
- DataGridView 的右键菜单(ContextMenuStrip)
- DataGridView使用技巧九:DataGridView的右键菜单(ContextMenuStrip)
- c#判断右键菜单(ContextMenuStrip)是从哪个控件弹出来的方法
- c#判断右键菜单(ContextMenuStrip)是从哪个控件弹出来的方法
- C#listview + 上下文菜单(鼠标右键菜单) ContextMenuStrip
- c#判断右键菜单(ContextMenuStrip)是从哪个控件弹出来的方法
- c#判断右键菜单(ContextMenuStrip)是从哪个控件弹出来的方法
- dataGridView上添加“右键菜单”
- [WinForm]DataGridView列头右键菜单
- 【转载于<小锋刚>博客园】C#遍历ContextMenuStrip右键菜单中包含子菜单的所有菜单并添加事件
- 右键下拉菜单contextMenuStrip的使用举例
- datagridview右键菜单
- 通过右键菜单复制只读的 DataGridView 单元格内容
- C# dataGridView右键菜单
- C#遍历ContextMenuStrip右键菜单中包含子菜单的所有菜单并添加事件