DataGridView.CellMouseClick 事件

定義

每當使用者以滑鼠按一下儲存格上的任何地方時發生。

public:
 event System::Windows::Forms::DataGridViewCellMouseEventHandler ^ CellMouseClick;
public event System.Windows.Forms.DataGridViewCellMouseEventHandler CellMouseClick;
public event System.Windows.Forms.DataGridViewCellMouseEventHandler? CellMouseClick;
member this.CellMouseClick : System.Windows.Forms.DataGridViewCellMouseEventHandler 
Public Custom Event CellMouseClick As DataGridViewCellMouseEventHandler 

事件類型

範例

下列程式碼範例示範如何使用這個成員。 在此範例中,事件處理常式會報告事件的發生次數 CellMouseClick 。 此報告可協助您瞭解事件發生的時間,並可協助您進行偵錯。 若要報告多個事件或經常發生的事件,請考慮將 取代 MessageBox.ShowConsole.WriteLine 或將訊息附加至多行 TextBox

若要執行範例程式碼,請將它貼入包含名為 DataGridView1 之類型 DataGridView 實例的專案。 然後,確定事件處理常式與 事件相關聯 CellMouseClick

private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Button", e.Button );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Clicks", e.Clicks );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "X", e.X );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Y", e.Y );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Delta", e.Delta );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Location", e.Location );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "CellMouseClick Event" );
}
Private Sub DataGridView1_CellMouseClick(sender as Object, e as DataGridViewCellMouseEventArgs) _ 
     Handles DataGridView1.CellMouseClick

    Dim messageBoxVB as New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Button", e.Button)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Clicks", e.Clicks)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "X", e.X)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Y", e.Y)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Delta", e.Delta)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Location", e.Location)
    messageBoxVB.AppendLine()
    MessageBox.Show(messageBoxVB.ToString(),"CellMouseClick Event")

End Sub

備註

當使用者按一下任何滑鼠按鍵時,就會發生此事件。 MouseEventArgs.Button使用 屬性來判斷按下的按鈕。

若要偵測儲存格內容的點選,例如儲存格內的按鈕或連結,請使用 CellContentClick 事件。

這個事件的處理常式會接收滑鼠指標位置的資料行索引和資料列索引。

對於 中的 DataGridViewCheckBoxCell 按一下,此事件會在核取方塊變更值之前發生,因此如果您不想根據目前值計算預期的值,您通常會改為處理 CellValueChanged 事件。 由於只有在認可使用者指定的值時才會發生該事件,通常發生在焦點離開儲存格時,您也必須處理 CurrentCellDirtyStateChanged 事件。 在該處理常式中,如果目前的儲存格是核取方塊儲存格,請呼叫 CommitEdit 方法並傳入 Commit 值。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱