//首先生成DataGridView的CellMouseDown事件
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            //循环遍历每一行,设置为不选中状态
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                this.dataGridView1.Rows[i].Selected = false;  
            }
            //设置当前鼠标单击或者右击的行为选中状态
            this.dataGridView1.Rows[e.RowIndex].Selected = true;

            //判断是不是右击
            if (e.Button== System.Windows.Forms.MouseButtons.Right)
            {
                //取到选中的行的某一单元格的值
                teacherid = this.dataGridView1.Rows[e.RowIndex].Cells["teacherid"].Value.ToString();

                //下面两种取值的方式是取不到值的
                //1.teacherid = this.dataGridView1.CurrentRow.Cells["teacherid"].Value.ToString();
                //2.teacherid = this.dataGridView1.CurrentCell.Value.ToString();
            }
        }


本文转载:CSDN博客