DataGridView 的右键菜单(ContextMenuStrip)
来源:互联网 发布:江苏运时数据待遇如何 编辑:程序博客网 时间:2024/06/11 10:00
一、使用属性
DataGridView, DataGridViewColumn, DataGridViewRow, DataGridViewCell 有 ContextMenuStrip 属性。可以通过设定 ContextMenuStrip 对象来控制 DataGridView 的右键菜单的显示。 DataGridViewColumn 的 ContextMenuStrip 属性设定了 除了列头以外的单元格的右键菜单。 DataGridViewRow 的 ContextMenuStrip 属性设定了除了行头以外的单元格的右键菜单。DataGridViewCell 的 ContextMenuStrip 属性设定了指定单元格的右键菜单。
[C#]
// DataGridView 的 ContextMenuStrip 设定
DataGridView1.ContextMenuStrip =
this
.ContextMenuStrip1;
// 列的 ContextMenuStrip 设定
DataGridView1.Columns[0].ContextMenuStrip =
this
.ContextMenuStrip2;
// 列头的 ContextMenuStrip 设定
DataGridView1.Columns[0].HeaderCell.ContextMenuStrip =
this
.ContextMenuStrip2;
// 行的 ContextMenuStrip 设定
DataGridView1.Rows[0].ContextMenuStrip =
this
.ContextMenuStrip3;
// 单元格的 ContextMenuStrip 设定
DataGridView1[0, 0].ContextMenuStrip =
this
.ContextMenuStrip4;
对于单元格上的右键菜单的设定,优先顺序是: Cell > Row > Column > DataGridView
二、使用dataGridView的 CellContextMenuStripNeeded、RowContextMenuStripNeeded 事件
利用 CellContextMenuStripNeeded 事件可以设定单元格的右键菜单,尤其但需要右键菜单根据单元格值的变化而变化的时候(例如你需要判断cell的值大于10时使用ContextMenuStrip1,小于10时使用ContextMenuStrip2 ,那就需要用CellContextMenuStripNeeded事件来设置了)。比起使用循环遍历,使用该事件来设定右键菜单的效率更高。但是,在DataGridView使用了DataSource绑定而且是VirtualMode的时候,该事件将不被引发。
[C#]
// CellContextMenuStripNeeded事件处理方法
private
void
DataGridView1_CellContextMenuStripNeeded(
object
sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if
(e.RowIndex < 0)
{
// 列头的ContextMenuStrip设定
e.ContextMenuStrip =
this
.ContextMenuStrip1;
}
else
if
(e.ColumnIndex < 0)
{
// 行头的ContextMenuStrip设定
e.ContextMenuStrip =
this
.ContextMenuStrip2;
}
else
if
(dgv[e.ColumnIndex, e.RowIndex].Value
is
int
)
{
// 如果单元格值是整数时
e.ContextMenuStrip =
this
.ContextMenuStrip3;
}
}
同样,可以通过 RowContextMenuStripNeeded 事件来设定行的右键菜单。
[C#]
// RowContextMenuStripNeeded事件处理方法
private
void
DataGridView1_RowContextMenuStripNeeded(
object
sender,
DataGridViewRowContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
// 当"Column1"列是Bool型且为True时、设定其的ContextMenuStrip
object
boolVal = dgv[
"Column1"
, e.RowIndex].Value;
Console.WriteLine(boolVal);
if
(boolVal
is
bool
&& (
bool
)boolVal)
{
e.ContextMenuStrip =
this
.ContextMenuStrip1;
}
}
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);
}
}
}
——————————————————再分割——————————————————————————————
对于treeview也有类似的问题,解决方法:
//对于treeview可以应用mousedown事务
办法一:
private
void
treeView1_MouseDown(
object
sender, MouseEventArgs e)
{
if
(e.Button == MouseButtons.Right)
{
TreeNode node =
this
.treeView1.GetNodeAt(e.Location);
if
(node !=
null
)
{
this
.treeView1.SelectedNode = node;
}
}
}
办法二:
void
jcsTreeView1_MouseDown(
object
sender, MouseEventArgs e)
{
System.Windows.Forms.TreeViewHitTestInfo hittestinfo =
this
.jcsTreeView1.HitTest(e.X ,e.Y);
if
(hittestinfo.Node !=
null
)
{
TreeViewHitTestLocations loc = hittestinfo.Location;
if
(loc == TreeViewHitTestLocations.Label )
MessageBox.Show(hittestinfo.Node.Text);
}
}
- DataGridView 的右键菜单(ContextMenuStrip)
- DataGridView 的右键菜单(ContextMenuStrip)
- [转]DataGridView 的右键菜单(ContextMenuStrip)
- DataGridView 的右键菜单(ContextMenuStrip)
- .NET给DataGridView添加右键选中并设置右键菜单ContextMenuStrip显示的位置
- C#高低文菜单(右键菜单)ContextMenuStrip用法总结
- C#listview + 上下文菜单(鼠标右键菜单) ContextMenuStrip
- 定制DataGridView快捷菜单ContextMenuStrip的关联事件
- 定制DataGridView快捷菜单ContextMenuStrip的关联事件
- datagridview中 ContextMenuStrip的使用
- PopupMenu和ContextMenuStrip右键菜单使用
- C#如何使用右键菜单【contextMenuStrip】
- arcengine onmouseup使用contextmenustrip添加右键菜单
- DataGridView 右键菜单复制(公共)
- C# dataGridView右键菜单
- C#DataGridView右键菜单
- DataGridView右键弹出菜单
- datagridview右键菜单
- LinkedHashMap和HashMap的比较使用
- ios json文件加载动态图,让app真正动起来
- 算法题
- 整理部分java虚拟机性能监测的命令
- 海量数据处理:十道面试题与十个海量数据处理方法总结
- DataGridView 的右键菜单(ContextMenuStrip)
- 运行安卓报Could not find ****.apk和Failed to load ****dx.jar错的解决办法
- c语言获取用户输入字符串是scanf和gets的区别
- FPGA进行串转并,并转串总结
- 树形查询SQL优化一例
- 错过双十一别郁闷 会声会影1212继续搞事儿
- Maven中使用MyBatis自动生成model实体、dao、XML映射文件
- 值传递与引用传递
- 由《成为乔布斯》联想而来