Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.

Evento DataGridView.CellClick


Data di pubblicazione: novembre 2016

Si verifica quando viene fatto clic su una qualsiasi parte di una cella.

Spazio dei nomi:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public event DataGridViewCellEventHandler CellClick

This event occurs when any part of a cell is clicked, including borders and padding. It also occurs when the user presses and releases the SPACE key while a button cell or check box cell has focus, and will occur twice for these cell types if the cell is clicked while pressing the SPACE key.

To determine when the cell contents are clicked, handle the E:System.Windows.Forms.DataGridView.CellContentClick event.

This event does not receive information about the mouse position. If the event handler needs information about the mouse position, use the E:System.Windows.Forms.DataGridView.CellMouseClick event.

For clicks in a T:System.Windows.Forms.DataGridViewCheckBoxCell, this event occurs before the check box changes value, so if you do not want to calculate the expected value based on the current value, you will typically handle the E:System.Windows.Forms.DataGridView.CellValueChanged event instead. Because that event occurs only when the user-specified value is committed, which typically occurs when focus leaves the cell, you must also handle the E:System.Windows.Forms.DataGridView.CurrentCellDirtyStateChanged event. In that handler, if the current cell is a check box cell, call the M:System.Windows.Forms.DataGridView.CommitEdit(System.Windows.Forms.DataGridViewDataErrorContexts) method and pass in the F:System.Windows.Forms.DataGridViewDataErrorContexts.Commit value.

For more information about handling events, see NIB: Consuming Events.

The following code example shows a E:System.Windows.Forms.DataGridView.CellClick event handler in a Tic-Tac-Toe game implementation that uses image columns in a T:System.Windows.Forms.DataGridView control. Unless the game is over or the cell has already been clicked, the event handler sets the cell value to one of two T:System.Drawing.Bitmap objects represented by the variables x and o.

This code is part of a larger example shown in How to: Work with Image Columns in the Windows Forms DataGridView Control.

private void dataGridView1_CellClick(object sender,
    DataGridViewCellEventArgs e)

    if (turn.Text.Equals(gameOverString)) { return; }

    DataGridViewImageCell cell = (DataGridViewImageCell)

    if (cell.Value == blank)
        if (IsOsTurn())
            cell.Value = o;
            cell.Value = x;
    if (IsAWin())
        turn.Text = gameOverString;

.NET Framework
Disponibile da 2.0
Torna all'inizio