DataGridView.CellClick DataGridView.CellClick DataGridView.CellClick DataGridView.CellClick Event

定義

發生於按一下儲存格的任何部分時。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 

範例

下列程式碼範例顯示CellClick在 Tic 中使用DataGridView控制項中的影像資料行時,該遊戲的事件處理常式。The following code example shows a CellClick event handler in a Tic-Tac-Toe game implementation that uses image columns in a DataGridView control. 除非遊戲已超過或已按下儲存格,否則事件處理常式會將資料格值設定為變數Bitmap xo所表示的兩個物件之一。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.

此程式碼屬於較大範例的一部分,如如何:使用 Windows Forms 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.

適用於

另請參閱