2008-10-06 62 views

回答

15
// Clear all the previously selected rows 
    foreach (DataGridViewRow row in yourDataGridView.Rows) 
    { 
     row.Selected = false; 
    } 

    // Get the selected Row 
    DataGridView.HitTestInfo info = yourDataGridView.HitTest(e.X, e.Y); 

    // Set as selected 
    yourDataGridView.Rows[info.RowIndex].Selected = true; 
+3

這會變得非常慢,當行量高... – karlipoppins 2010-04-04 18:56:58

+2

如果您的DataGridView多選有設置爲false,然後清除以前的選擇是不必要的。另外,如果命中不是有效的行/列,HitTestInfo可以返回HitTestInfo.Nowhere。 – stuartd 2010-10-21 14:28:21

5

的很酷的事情是添加一個菜單上單擊鼠標右鍵,例如包含「查看客戶信息」選項,「確認最後發票」,「添加日誌條目以這個客戶端」等

你只需要添加一個ContextMenuStrip對象,添加你的菜單條目,並在DataGridView屬性中選擇它的ContextMenuStrip。

這將創建用戶權限的所有選項單擊該行中一個新的菜單,那麼所有你需要做的是讓你的法寶:)

記住,你需要傑威爾代碼得到了什麼行用戶進入,然後獲取包含客戶端ID的單元格並傳遞該信息。

希望它有助於提高你的應用程序

http://img135.imageshack.us/img135/5246/picture1ku5.png

http://img72.imageshack.us/img72/6038/picture2lb8.png

0

您可以使用傑威爾的代碼在你的DataGridView的MouseDown事件。

3

子類的DataGridView並創建一個MouseDown事件網格,


private void SubClassedGridView_MouseDown(object sender, MouseEventArgs e) 
{ 
    // Sets is so the right-mousedown will select a cell 
    DataGridView.HitTestInfo hti = this.HitTest(e.X, e.Y); 
    // Clear all the previously selected rows 
    this.ClearSelection(); 

    // Set as selected 
    this.Rows[hti.RowIndex].Selected = true; 
} 
17

讓它表現類似於鼠標左鍵?例如

private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Right) 
    { 
     dataGridView.CurrentCell = dataGridView[e.ColumnIndex, e.RowIndex]; 
    } 
} 
0

你必須做兩件事情:

  1. 清除所有的行,並選擇當前。我經歷了所有行中循環,並使用布爾表達式i = e.RowIndex

  2. 如果你已經做了第1步,你仍然有一個很大的缺陷:
    DataGridView1.CurrentRow不會返回之前選擇的行(這是相當危險的)。由於CurrentRow是隻讀的,你所要做的

    Me.CurrentCell = Me.Item(e.ColumnIndex, e.RowIndex)

    Protected Overrides Sub OnCellMouseDown(
        ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) 
    
        MyBase.OnCellMouseDown(e) 
    
        Select Case e.Button 
         Case Windows.Forms.MouseButtons.Right 
          If Me.Rows(e.RowIndex).Selected = False Then 
           For i As Integer = 0 To Me.RowCount - 1 
            SetSelectedRowCore(i, i = e.RowIndex) 
           Next 
          End If 
    
          Me.CurrentCell = Me.Item(e.ColumnIndex, e.RowIndex) 
        End Select 
    
    End Sub 
    
相關問題