日期:2014-05-20  浏览次数:20784 次

DateGridView 那个事件是响应鼠标右键的???
我想鼠标右键单击一行,然后这行被选中,再弹出快捷菜单。
快捷菜单是系统默认可以的,请问怎么选中点击的行?

------解决方案--------------------
添加如下的事件处理:
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.dataGridView1.CurrentCell = this.dataGridView1[e.ColumnIndex, e.RowIndex];
}
}
------解决方案--------------------
首先你要一行被选中的话,需要设定this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

其次,判断鼠标点击事件应该在MouseUp中,而不是MouseDown.因为快捷菜单最终是在鼠标释放后才出来的,而不是一按下鼠标就出来.你可以随便找个地方看下就知道了.

dataGridView1.CellMouseUp += new DataGridViewCellMouseEventHandler(m_gridControl_CellMouseUp);
private void m_gridControl_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.dataGridView1.CurrentCell = this.dataGridView1[e.ColumnIndex, e.RowIndex];
}
}