1.为dataGridView绑定数据,设置数据选定一行
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //选中整行
2.添加contextMenuStrip控件并绑定dataGridView
3.设置选中一行时右键单击出现菜单
//dataGridView选中一行时右键出现菜单
private void dataGridView1_CellMouseUp(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);
}
}
}