在datagridview中有时需要在右键点击某行的时候就选中它,那么我们只需要在datagridview的CellMonseDown事件中添加如下代码就行:
if (e.Button == MouseButtons.Right && e.RowIndex > -1 && e.ColumnIndex > -1)
{
(sender as DataGridView).CurrentRow.Selected = false;
(sender as DataGridView).Rows[e.RowIndex].Selected = true;
}