DataGridView.CellClick 事件

定义

在单元格的任何部分被单击时发生。Occurs when any part of a cell is clicked.

public:
 event System::Windows::Forms::DataGridViewCellEventHandler ^ CellClick;
public event System.Windows.Forms.DataGridViewCellEventHandler CellClick;
member this.CellClick : System.Windows.Forms.DataGridViewCellEventHandler 
Public Custom Event CellClick As DataGridViewCellEventHandler 

事件类型

DataGridViewCellEventHandler

示例

下面的代码示例演示了 CellClick 使用控件中的图像列的票游戏实现中的事件处理程序 DataGridViewThe following code example shows a CellClick event handler in a Tic-Tac-Toe game implementation that uses image columns in a DataGridView control. 除非游戏结束或已单击该单元格,否则,该事件处理程序会将单元值设置为由 Bitmap 变量和表示的两个对象中的一个 x oUnless 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.

此代码是在 如何:处理 Windows 窗体 DataGridView 控件中的图像列的更大示例的一部分。This code is part of a larger example shown in How to: Work with Image Columns in the Windows Forms DataGridView Control.

void dataGridView1_CellClick( Object^ sender, DataGridViewCellEventArgs^ e )
{
   if ( turn->Equals( gameOverString ) )
   {
      return;
   }

   DataGridViewImageCell^ cell = dynamic_cast<DataGridViewImageCell^>(dataGridView1->Rows[ e->RowIndex ]->Cells[ e->ColumnIndex ]);
   if ( cell->Value == blank )
   {
      if ( IsOsTurn() )
      {
         cell->Value = o;
      }
      else
      {
         cell->Value = x;
      }

      ToggleTurn();
   }

   if ( IsAWin( cell ) )
   {
      turn->Text = gameOverString;
   }
}


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;
    }
}
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

注解

在单击单元格的任何部分(包括边框和填充)时发生此事件。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.

若要确定何时单击单元格内容,请处理 CellContentClick 事件。To determine when the cell contents are clicked, handle the CellContentClick event.

此事件不会接收有关鼠标位置的信息。This event does not receive information about the mouse position. 如果事件处理程序需要有关鼠标位置的信息,请使用 CellMouseClick 事件。If the event handler needs information about the mouse position, use the CellMouseClick event.

对于在中单击的 DataGridViewCheckBoxCell ,此事件发生在复选框更改值之前,因此,如果您不希望基于当前值计算预期值,则通常会处理 DataGridView.CellValueChanged 事件。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. 由于该事件仅在提交用户指定的值时才发生,这通常发生在焦点离开单元格时,您还必须处理 DataGridView.CurrentCellDirtyStateChanged 事件。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. 在该处理程序中,如果当前单元格为复选框单元,则调用 DataGridView.CommitEdit 方法并传入 Commit 值。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 how to handle events, see Handling and Raising Events.

适用于

另请参阅