2017-08-08 11 views
1

行を選択せず​​にcontextmenuDataGridViewに表示する方法はありますか?私はDataGridViewcontextmenuを両方の方法で表示するには、行を選択し、行を選択しないでください。ここに選択した行にcontextmenuを表示するための私のコードです。行を選択せず​​にdataGridViewを右クリック

ご協力いただければ幸いです。

private void ProductServicesDataGrid_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Right) 
     { 
      var hti = ProductServicesDataGrid.HitTest(e.X, e.Y); 
      ProductServicesDataGrid.Rows[hti.RowIndex].Selected = true; 

      ProductContextMenu.Show(ProductServicesDataGrid, e.X, e.Y); 
     } 
    } 
+0

[DataGridViewのための右クリックコンテキストメニュー](https://stackoverflow.com/questions/1718389/right-click-context-menu-for-datagridview) – Sahin

+1

_」の可能性のある重複はする方法はあります行を選択せず​​にDataGridViewにコンテキストメニューを表示する "_...はい...' DataGridViews'' ContextMenuStrip'を使用すると、グリッドを右クリックしてコンテキストメニューを表示できます。グリッドを右クリックすると、セルが選択または選択解除されません。 – JohnG

答えて

1

これが役に立たなかった場合は、コードを少し編集してみましょう。

private void ProductServicesDataGrid_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (e.Button == System.Windows.Forms.MouseButtons.Right) 
      { 
       if(ProductServicesDataGrid.SelectedCells!=null) 
       { 
        // you can use selected rows in a foreach loop however you want       
       ProductContextMenu = new ProductContextMenu(); 
        foreach (DataGridViewCell cell in ProductServicesDataGrid.SelectedCells) 
         { 
         m.MenuItems.Add(new MenuItem(cell.Value)); 
         } 
         ProductContextMenu.Show(ProductServicesDataGrid, e.X, e.Y); 
       } 
       else 
       { 
       // any cells are selected 
       } 
      } 
     } 
関連する問題