设置了DataGridView的列的ContextMenuStrip,点右键弹出快捷菜单后,如何得到被点击的是DataGridView的那一列呢?

解决方案 »

  1.   

    在 Item_Create 事件里
    写入
    e.Item.Cells[0].Attributes.Add("oncontextmenu","javascript Function");
      

  2.   

    以前的 oncontextmenu表示弹出右健菜单
      

  3.   

    int x = DataGridView.currentrow.index;
      

  4.   

    谢谢fayewang(飞狐) ,不过实在WinForm中。有什么办法吗?
      

  5.   

    irow = dgv_DataList.CurrentRow.Index;
                if (irow != -1)
                {
                    string x1 = dgv_DataList[0, irow].Value.ToString();
                    string x2 = dgv_DataList[1, irow].Value.ToString();
                    string x3 = dgv_DataList[2, irow].Value.ToString();
                    string x4 = dgv_DataList[3, irow].Value.ToString();
      

  6.   

    在你datagridview的MouseUp或者MouseDown事件中,如下进行判断
    http://www.syncfusion.com/faq/windowsforms/search/689.aspx如上的方法虽说是在DataGrid,也适用于datagridview。
      

  7.   

    我的一个做法, 供参考: 
           
    private void dataGridView1_RowContextMenuStripNeeded(object sender, DataGridViewRowContextMenuStripNeededEventArgs e)
    {
    // 获取当前行
    DataGridViewRow iRow = dataGridView1.Rows[e.RowIndex];
    // 弹出右键菜单
    if (iRow.Selected)
       e.ContextMenuStrip = contextMenuStrip1;   // contextMenuStrip1为已定义好的右键菜单
    else
    {
       // 取消所有已选择的行
       dataGridView1.ClearSelection();
       // 取消当前单元格
       dataGridView1.CurrentCell = null;
       // 设置当前行选择
       iRow.Selected = true;
       // 设置快捷菜单
       e.ContextMenuStrip = contextMenuStrip1;
    }
    }
      

  8.   

    在mousedown事件进行计算,鼠标在datagridview里的坐标和cols.width算一下就出来的,很快的
      

  9.   

    。上面方法都是陷入LZ的问题了,不能改个方法吗?
    在CellClick或者其他类似事件中,让菜单Show,而不是在菜单显示后,再判断吗?
      

  10.   

    mVehicleInfo.VehicleID = dgvVehicleInfoShow.CurrentRow.Cells["vehicle_id"].Value.ToString().Trim();
                    mVehicleInfo.LicenseTagNo = dgvVehicleInfoShow.CurrentRow.Cells["license_tag_no"].Value.ToString().Trim();
                    mVehicleTypeInfo.TypeName = dgvVehicleInfoShow.CurrentRow.Cells["type_name"].Value.ToString().Trim();
                    mVehicleInfo.OriginalEngineID = dgvVehicleInfoShow.CurrentRow.Cells["original_engine_id"].Value.ToString().Trim();
                    mVehicleInfo.OriginalEngineType = dgvVehicleInfoShow.CurrentRow.Cells["original_engine_type"].Value.ToString().Trim();
                    mVehicleInfo.ChangedEngineID = dgvVehicleInfoShow.CurrentRow.Cells["changed_engine_id"].Value.ToString().Trim();
    这样几行代码不知道对楼主有没有用
      

  11.   

    this.datagridview.currentRow.Cell[this.datagridV
    iew.CurrentCell].value