您的位置:首页 > 编程语言 > C#

C#中为DataGrid显示右键菜单与右键切换当前行的问题

2011-12-13 15:27 399 查看
C#中为DataGrid显示右键菜单与右键切换当前行的问题

部分摘录自HadyBlog,请见文后完整文章链接

private void DataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

{

if (e.Button == MouseButtons.Right)

{

if (e.RowIndex >= 0)

{

//若行已是选中状态就不再进行设置

if (dataGridView1.Rows[e.RowIndex].Selected == false)

{

dataGridView1.ClearSelection();

dataGridView1.Rows[e.RowIndex].Selected = true;

}

//只选中一行时设置活动单元格,同时切换当前行

if (dataGridView1.SelectedRows.Count == 1)

{

dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

}

//弹出操作菜单

contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);

}

}

}

这样在处理右键菜单的菜单项点击事件的时候就可以使用dataGridView1.CurrentRow.Index来获取右键所在行的序号了

完整文章请见:http://blog.sina.com.cn/s/blog_4b4113e40100gddf.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: