C# dataGridView右键菜单
2010-11-11 23:12
309 查看
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];
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
}
在DataGridView中的CurrentRow属性为只读,且其Index也不能动态设置,故只能在DataGridView中用左键来选择行,从而实现当前行的定位。
现在要实现在DataGridView中单击右键实现左键的功能,代码如下
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right && e.RowIndex > -1 && e.ColumnIndex > -1)
{
dataGridView1.CurrentRow.Selected = false;
dataGridView1.Rows[e.RowIndex].Selected = true;
}
}
DatagridView的CellMouseDown事件添加如上代码,在不考虑注释代码的情况下,可以实现对当前选中行的不显示选中,而对鼠标右击的行实现选中
这样存在一个问题,CurrentRow的属性仍然为之前的哪个值,即使将鼠标右键选中的行的Selected设置为True也不能改变。
而在将注释代码注销后即可同时改变CurrentRow的属性,这样以后编码方便多了!
当然在对CurrentCell赋值的时候别忘了判断鼠标右击到DataGridView边框行列的情况
{
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);
}
}
}
在DataGridView中的CurrentRow属性为只读,且其Index也不能动态设置,故只能在DataGridView中用左键来选择行,从而实现当前行的定位。
现在要实现在DataGridView中单击右键实现左键的功能,代码如下
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right && e.RowIndex > -1 && e.ColumnIndex > -1)
{
dataGridView1.CurrentRow.Selected = false;
dataGridView1.Rows[e.RowIndex].Selected = true;
}
}
DatagridView的CellMouseDown事件添加如上代码,在不考虑注释代码的情况下,可以实现对当前选中行的不显示选中,而对鼠标右击的行实现选中
这样存在一个问题,CurrentRow的属性仍然为之前的哪个值,即使将鼠标右键选中的行的Selected设置为True也不能改变。
而在将注释代码注销后即可同时改变CurrentRow的属性,这样以后编码方便多了!
当然在对CurrentCell赋值的时候别忘了判断鼠标右击到DataGridView边框行列的情况
相关文章推荐
- C# DataGridView添加右键菜单等技巧(转)
- C#DataGridView右键菜单
- C# winform datagridview中如何实现鼠标右键点击一行数据出现一个带有删除的菜单,并能执行删除操作?
- C# DataGridView添加右键菜单等技巧
- C# DataGridView添加右键菜单等技巧
- C#Winform基础 为datagridview添加右键菜单
- 【转】C# DataGridView添加右键菜单等技巧
- C#Winform基础 为datagridview添加右键菜单
- DataGridView右键选中某单元格(C# Winfrom)
- C#实现窗体最小化时存至系统托盘,并为notifyicon添加右键菜单
- C# 如何在 Windows 文件系统中添加自定义右键菜单
- c#小软件(SaveClassic)开发手记--(2)数据设计,IE右键菜单调用exe功能
- c# ListView右键菜单事件
- C#进行Visio二次开发之自定义右键菜单
- 如何在C#添加鼠标右键菜单
- DataGridView 右键菜单复制(公共)
- C#当用户在ritchbox中单击鼠标右键时,弹出一个快捷菜单,单击某一地名,在ritchbox中显示相关信息
- C# treeview右键菜单 设置技巧
- C#上下文菜单(右键菜单)ContextMenuStrip用法总结
- paip.c#.net 右键菜单带图标