DataGridView.CellContentClick Événement

Définition

Se produit lors d'un clic sur le contenu d'une cellule.

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

Type d'événement

Exemples

L’exemple de code suivant fournit un gestionnaire pour cet événement qui détermine si la cellule cliquée est une cellule de lien ou une cellule de bouton et effectue l’action correspondante en conséquence. Cet exemple fait partie d’un exemple plus large disponible dans la rubrique vue d’ensemble de la DataGridViewComboBoxColumn classe.

private:
    void DataGridView1_CellContentClick(Object^ /*sender*/, DataGridViewCellEventArgs^ e)
    {

        if (IsANonHeaderLinkCell(e))
        {
            MoveToLinked(e);
        }
        else if (IsANonHeaderButtonCell(e))
        {
            PopulateSales(e);
        }
    }

private:
    void MoveToLinked(DataGridViewCellEventArgs^ e)
    {
        String^ employeeId;
        Object^ value = DataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value;
        if (dynamic_cast<DBNull^>(value) != nullptr) { return; }

        employeeId = value->ToString();
        DataGridViewCell^ boss = RetrieveSuperiorsLastNameCell(employeeId);
        if (boss != nullptr)
        {
            DataGridView1->CurrentCell = boss;
        }
    }

private:
    bool IsANonHeaderLinkCell(DataGridViewCellEventArgs^ cellEvent)
    {
        if (dynamic_cast<DataGridViewLinkColumn^>(DataGridView1->Columns[cellEvent->ColumnIndex]) != nullptr
             &&
            cellEvent->RowIndex != -1)
        { return true; }
        else { return false; }
    }

private:
    bool IsANonHeaderButtonCell(DataGridViewCellEventArgs^ cellEvent)
    {
        if (dynamic_cast<DataGridViewButtonColumn^>(DataGridView1->Columns[cellEvent->ColumnIndex]) != nullptr
             &&
            cellEvent->RowIndex != -1)
        { return true; }
        else { return (false); }
    }

private:
    DataGridViewCell^ RetrieveSuperiorsLastNameCell(String^ employeeId)
    {

        for each (DataGridViewRow^ row in DataGridView1->Rows)
        {
            if (row->IsNewRow) { return nullptr; }
            if (row->Cells[ColumnName::EmployeeID.ToString()]->Value->ToString()->Equals(employeeId))
            {
                return row->Cells[ColumnName::LastName.ToString()];
            }
        }
        return nullptr;
    }
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

    if (IsANonHeaderLinkCell(e))
    {
        MoveToLinked(e);
    }
    else if (IsANonHeaderButtonCell(e))
    {
        PopulateSales(e);
    }
}

private void MoveToLinked(DataGridViewCellEventArgs e)
{
    string employeeId;
    object value = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
    if (value is DBNull) { return; }

    employeeId = value.ToString();
    DataGridViewCell boss = RetrieveSuperiorsLastNameCell(employeeId);
    if (boss != null)
    {
        DataGridView1.CurrentCell = boss;
    }
}

private bool IsANonHeaderLinkCell(DataGridViewCellEventArgs cellEvent)
{
    if (DataGridView1.Columns[cellEvent.ColumnIndex] is
        DataGridViewLinkColumn &&
        cellEvent.RowIndex != -1)
    { return true; }
    else { return false; }
}

private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
{
    if (DataGridView1.Columns[cellEvent.ColumnIndex] is
        DataGridViewButtonColumn &&
        cellEvent.RowIndex != -1)
    { return true; }
    else { return (false); }
}

private DataGridViewCell RetrieveSuperiorsLastNameCell(string employeeId)
{

    foreach (DataGridViewRow row in DataGridView1.Rows)
    {
        if (row.IsNewRow) { return null; }
        if (row.Cells[ColumnName.EmployeeId.ToString()].Value.ToString().Equals(employeeId))
        {
            return row.Cells[ColumnName.LastName.ToString()];
        }
    }
    return null;
}
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles DataGridView1.CellContentClick

    If IsANonHeaderLinkCell(e) Then
        MoveToLinked(e)
    ElseIf IsANonHeaderButtonCell(e) Then
        PopulateSales(e)
    End If
End Sub

Private Sub MoveToLinked(ByVal e As DataGridViewCellEventArgs)
    Dim employeeId As String
    Dim value As Object = DataGridView1.Rows(e.RowIndex). _
        Cells(e.ColumnIndex).Value
    If value.GetType Is GetType(DBNull) Then Return

    employeeId = CType(value, String)
    Dim boss As DataGridViewCell = _
        RetrieveSuperiorsLastNameCell(employeeId)
    If boss IsNot Nothing Then
        DataGridView1.CurrentCell = boss
    End If
End Sub

Private Function IsANonHeaderLinkCell(ByVal cellEvent As _
    DataGridViewCellEventArgs) As Boolean

    If TypeOf DataGridView1.Columns(cellEvent.ColumnIndex) _
        Is DataGridViewLinkColumn _
        AndAlso Not cellEvent.RowIndex = -1 Then _
        Return True Else Return False

End Function

Private Function IsANonHeaderButtonCell(ByVal cellEvent As _
    DataGridViewCellEventArgs) As Boolean

    If TypeOf DataGridView1.Columns(cellEvent.ColumnIndex) _
        Is DataGridViewButtonColumn _
        AndAlso Not cellEvent.RowIndex = -1 Then _
        Return True Else Return (False)

End Function

Private Function RetrieveSuperiorsLastNameCell( _
    ByVal employeeId As String) As DataGridViewCell

    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.IsNewRow Then Return Nothing
        If row.Cells(ColumnName.EmployeeId.ToString()). _
            Value.ToString().Equals(employeeId) Then
            Return row.Cells(ColumnName.LastName.ToString())
        End If
    Next
    Return Nothing
End Function

Remarques

Cet événement se produit lorsque vous cliquez sur le contenu de la cellule. Cela se produit également lorsque l’utilisateur appuie et relâche la barre d’espace alors qu’une cellule de bouton ou case activée cellule de zone a le focus, et se produit deux fois pour ces types de cellules si vous cliquez sur le contenu de la cellule tout en appuyant sur la barre d’espace.

Utilisez cet événement pour détecter les clics de bouton pour un DataGridViewButtonCell ou les clics de lien pour un DataGridViewLinkCell.

Pour les clics dans un DataGridViewCheckBoxCell, cet événement se produit avant que la zone de case activée change de valeur. Par conséquent, si vous ne souhaitez pas calculer la valeur attendue en fonction de la valeur actuelle, vous allez généralement gérer l’événement à la DataGridView.CellValueChanged place. Étant donné que cet événement se produit uniquement lorsque la valeur spécifiée par l’utilisateur est validée, ce qui se produit généralement lorsque le focus quitte la cellule, vous devez également gérer l’événement DataGridView.CurrentCellDirtyStateChanged . Dans ce gestionnaire, si la cellule active est une cellule de zone de case activée, appelez la DataGridView.CommitEdit méthode et transmettez la Commit valeur.

Pour plus d’informations sur la façon de gérer les événements, consultez gestion et déclenchement d’événements.

S’applique à

Voir aussi