我想鼠标右键单击一行,然后这行被选中,再弹出快捷菜单。
快捷菜单是系统默认可以的,请问怎么选中点击的行?

解决方案 »

  1.   

    添加如下的事件处理:
    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
    if (e.Button == MouseButtons.Right)
    {
    this.dataGridView1.CurrentCell = this.dataGridView1[e.ColumnIndex, e.RowIndex];
    }
    }
      

  2.   

    首先你要一行被选中的话,需要设定this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;其次,判断鼠标点击事件应该在MouseUp中,而不是MouseDown.因为快捷菜单最终是在鼠标释放后才出来的,而不是一按下鼠标就出来.你可以随便找个地方看下就知道了.dataGridView1.CellMouseUp += new DataGridViewCellMouseEventHandler(m_gridControl_CellMouseUp);
    private void m_gridControl_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
       {
           this.dataGridView1.CurrentCell = this.dataGridView1[e.ColumnIndex, e.RowIndex];
       }
    }
      

  3.   

    两位都漏了一句:
      
      dataGridView1.Rows[e.RowIndex].Selected = true;//使行处于选中状态
      

  4.   

    设置了currentcell后,这个行自然就是selected = true了. 不需要再设置.