2013-05-27 65 views
8

Tôi cần chọn một hàng trong dataGridView bằng cách nhấp chuột phải trước khi ContextMenu hiển thị vì contextMenu là hàng phụ thuộc.Nhấp chuột phải để chọn hàng trong dataGridView

Tôi đã thử điều này:

if (e.Button == MouseButtons.Right) 
     { 

      var hti = dataGrid.HitTest(e.X, e.Y); 
      dataGrid.ClearSelection(); 
      dataGrid.Rows[hti.RowIndex].Selected = true; 
     } 

hay:

private void dataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Right) 
     { 
      dataGrid.Rows[e.RowIndex].Selected = true; 
      dataGrid.Focus(); 
     } 
    } 

này làm việc nhưng khi tôi cố gắng đọc dataGrid.Rows [CurrentRow.Index] Tôi chỉ thấy hàng đã chọn với trái bấm và không được chọn với nhấp chuột phải ..

Trả lời

22

Thử đặt ô hiện tại như thế này (điều này sẽ đặt thuộc tính CurrentRow của DataGridView trước ngữ cảnh của nam giới u mục được chọn):

private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Right) 
     { 
      // Add this 
      dataGrid.CurrentCell = dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex]; 
      // Can leave these here - doesn't hurt 
      dataGrid.Rows[e.RowIndex].Selected = true; 
      dataGrid.Focus(); 
     } 

    } 
+0

Cảm ơn, công trình này. – user2396911

+0

Bạn được chào đón! – Gjeltema

2

Tôi nhận thấy chủ đề này cũ, tôi chỉ muốn thêm một điều: Nếu bạn muốn có thể chọn và thực hiện tác vụ trên nhiều hàng: bạn có thể kiểm tra xem liệu hàng bạn đang nhấp chuột phải đã được chọn chưa. Bằng cách này, DataGridview hoạt động giống như một ListView trong lĩnh vực này. Vì vậy, nhấp chuột phải vào một hàng chưa được chọn: chọn hàng này và mở menu ngữ cảnh. Nhấp chuột phải vào một hàng đã được chọn chỉ cung cấp cho bạn menu ngữ cảnh và giữ cho các hàng đã chọn như mong đợi.

private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     if (e.RowIndex != -1 && e.ColumnIndex != -1) 
     { 
      if (e.Button == MouseButtons.Right) 
      { 
       DataGridViewRow clickedRow = (sender as DataGridView).Rows[e.RowIndex]; 
       if (!clickedRow.Selected) 
        dataGridView1.CurrentCell = clickedRow.Cells[e.ColumnIndex]; 

       var mousePosition = dataGridView1.PointToClient(Cursor.Position); 

       ContextMenu1.Show(dataGridView1, mousePosition); 
      } 
     } 
    } 
0
private void grid_listele_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Right) 
     { 
      grid_listele.ClearSelection(); 
      grid_listele[e.ColumnIndex, e.RowIndex].Selected = true; 
     } 


    } 
+0

Bạn có thể thêm giải thích là tại sao ví dụ của OP không hoạt động và những gì bạn đã làm không? – Yogesh

0
if (e.Button == System.Windows.Forms.MouseButtons.Right) 
     { 
      var hti = GridView.HitTest(e.X, e.Y); 
      GridView.ClearSelection(); 

      int index = hti.RowIndex; 

      if (index >= 0) 
      { 
       GridView.Rows[hti.RowIndex].Selected = true; 
       LockFolder_ContextMenuStrip.Show(Cursor.Position); 
      } 

     } 

Đây là phương pháp chính xác i Đoán

Các vấn đề liên quan