我指定了单元格的右键菜单:
DataGridViewRow row = (DataGridViewRow)this.dataGridView1.RowTemplate.Clone();
DataGridViewTextBoxCell celluser = new DataGridViewTextBoxCell();
cell.ContextMenuStrip = contextMenuStrip1;但是我想在菜单点击事件中获得点击的那个单元格在哪一列、哪一行,请高手指点一下小弟。
 private void 右键按钮1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //如何获得所在列和行,包括单元格的内容???????
        }

解决方案 »

  1.   

    Point   p=new   Point(contextMenuStrip1.Left,   contextMenuStrip1.Top); 
    for(int   i   =   0;   i   <   dataGridView1.Columns.Count;   i++) 

       Rectangle   columnrect   =   dataGridView1.GetColumnDisplayRectangle(i,   false); 
       if(dataGridView1.RectangleToScreen(columnrect).Contains(p)) 
        { 
               break; 
        } 
    }
      

  2.   

    try...private void dataGridView1_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
    {
        dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
    }private void 右键按钮1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (dataGridView1.CurrentCell != null)
        {
           textBox1.Text = dataGridView1.CurrentCell.RowIndex.ToString();     //行索引
            textBox2.Text = dataGridView1.CurrentCell.ColumnIndex.ToString();  //列索引
            textBox3.Text = dataGridView1.CurrentCell.Value.ToString();        //内容
        }            
    }
      

  3.   


    DataGridViewCell cell;  //MouseRightClick位置的单元格,不一定是当前单元格(dataGridView1.CurrentCell)
    private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            DataGridView.HitTestInfo gp = ((DataGridView)sender).HitTest(e.X, e.Y);
            if (gp.RowIndex > -1 && gp.RowIndex>-1)
            {
                cell = ((DataGridView)sender)[gp.ColumnIndex, gp.RowIndex];  // 在右键菜单事件 访问 cell
                MessageBox.Show("第" + gp.RowIndex + "行,第" + gp.ColumnIndex + "列");
            }
        }
    }
      

  4.   

    更正一下6楼代码
    if (gp.RowIndex > -1 && gp.RowIndex>-1)
    {
       DataGridView.HitTestInfo gp = ((DataGridView)sender).HitTest(e.X, e.Y);
       cell = ((DataGridView)sender)[gp.ColumnIndex, gp.RowIndex];  // 在右键菜单事件 访问 cell
       MessageBox.Show("第" + gp.RowIndex + "行,第" + gp.ColumnIndex + "列");
    }