在DataGridView中,先用左键选中一个单元格,然后再右键点击其他列的列名(columnHeaders),在弹出的右键中选择对该列的处理方式。
但是现在,焦点一直在原来左键选定的单元格,对该列的处理变成对选定单元格那列的处理。请问有没有什么好的办法能解决点击右键获得焦点的问题?望高手指点。

解决方案 »

  1.   

    private void dataGridView1_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
    {
        dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
    }private void 右键ToolStripMenuItem_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();        
        }            
    }
      

  2.   

    谢谢啦。
    我试了下,虽然能处理事件,不过它的游标的focus还是停留在原来的单元格里。
    他的要求是,直接能让被点击列名的那列,直接获得focus,并对该列进行处理(比如全选,复制等等),我觉的就像要实现跟点击左键一样的功能,并且还得显示下拉框。
    你看能实现吗?再次感谢
      
      

  3.   


            #region DataGridView 右键选中
            /// <summary>
            /// DataGridView 右键选中
            /// </summary>
            /// <param name="e"></param>
            private void DataGridMen(DataGridViewCellMouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (e.RowIndex >= 0)
                    {
                        spxxGridView.ClearSelection();
                        spxxGridView.Rows[e.RowIndex].Selected = true;
                        spxxGridView.CurrentCell = spxxGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        jyMenu.Checked = Load_spzt(e.RowIndex);
                        DataGrivMenu.Show(MousePosition.X, MousePosition.Y); 
                    }
                }
            }
            #endregion
      

  4.   


     private void dgvRoomInfos_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
         if (e.Button == MouseButtons.Right)//当按下鼠标右键时
          {
            this.dgvRoomInfos.SelectedRows[0].Selected = false;//原来选中行设为false
                            this.dgvRoomInfos.Rows[e.RowIndex].Selected = true;//单击右键这行设为选中
          }
    }
      

  5.   

    Thanks
    不过怎么还是不行啊,焦点还是移不过去