Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
DataGridView Class
DataGridView Events
 CellClick Event

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
DataGridView..::.CellClick Event

Occurs when any part of a cell is clicked.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
Public Event CellClick As DataGridViewCellEventHandler
Visual Basic (Usage)
Dim instance As DataGridView
Dim handler As DataGridViewCellEventHandler

AddHandler instance.CellClick, handler
C#
public event DataGridViewCellEventHandler CellClick
Visual C++
public:
 event DataGridViewCellEventHandler^ CellClick {
    void add (DataGridViewCellEventHandler^ value);
    void remove (DataGridViewCellEventHandler^ value);
}
JScript
JScript does not support events.

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 CellContentClick event.

This event does not receive information about the mouse position. If the event handler needs information about the mouse position, use the CellMouseClick event.

For clicks in a 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 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 DataGridView..::.CurrentCellDirtyStateChanged event. In that handler, if the current cell is a check box cell, call the DataGridView..::.CommitEdit method and pass in the Commit value.

For more information about handling events, see Consuming Events.

The following code example shows a CellClick event handler in a Tic-Tac-Toe game implementation that uses image columns in a 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 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.

Visual Basic
Private Sub dataGridView1_CellClick(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles dataGridView1.CellClick

    If turn.Text.Equals(gameOverString) Then Return

    Dim cell As DataGridViewImageCell = _
        CType(dataGridView1.Rows(e.RowIndex). _
            Cells(e.ColumnIndex), DataGridViewImageCell)
    If (cell.Value Is blank) Then
        If IsOsTurn() Then
            cell.Value = o
        Else
            cell.Value = x
        End If
        ToggleTurn()
        ToolTip(e)
    End If
    If IsAWin() Then
        turn.Text = gameOverString
    End If
End Sub

C#
private void dataGridView1_CellClick(object sender,
    DataGridViewCellEventArgs e)
{

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

    DataGridViewImageCell cell = (DataGridViewImageCell)
        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker