C# datagridview內 单击右键,如何获得右键单击处datagridview单元格的值????现在我的datagridview里面显示了数据,我需要右击datagridview然后得到点击处datagridview的值,然后根据这个值 做其他的操作。

解决方案 »

  1.   

    I think this will helpful.http://stackoverflow.com/questions/173295/right-click-to-select-a-datagridview-row
      

  2.   

    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right)
                    object selectvalue= this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            }
      

  3.   

     and up
    I think the second floor is right.
      

  4.   

    感谢你的答复,现在可以得到值但是如何控制右键菜单出现的位置为鼠标点击的地方呢?也就是怎样得到点击的那个cell控件。
      

  5.   

     private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
            {
                int r = this.dataGridView1.HitTest(e.X, e.Y).RowIndex;
                int c = this.dataGridView1.HitTest(e.X, e.Y).ColumnIndex;
                if (r >= 0 && c >= 0)   //点中Cell
                {
                    DataGridViewCell hitcell = this.dataGridView1.Rows[r].Cells[c];                Point pscreen = this.dataGridView1.PointToScreen(e.Location);
                    Point pForm = this.PointToClient(pscreen);
                    this.textBox1.Text = pscreen.ToString();  //相对屏幕的坐标
                    this.textBox2.Text = pForm.ToString();    //相对当前Form的坐标            }
                else  //没点中Cell
                {
                    
                }        }
      

  6.   

    ok,thank you very much!!!