DataGridView.CellContentClick Evento

Definición

Tiene lugar cuando se hace clic en el contenido de una celda.

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 

Tipo de evento

Ejemplos

En el ejemplo de código siguiente se proporciona un controlador para este evento que determina si la celda con clic es una celda de vínculo o una celda de botón y realiza la acción correspondiente como resultado. Este ejemplo forma parte de un ejemplo más grande disponible en el tema de información general de la DataGridViewComboBoxColumn clase.

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

Comentarios

Este evento se produce cuando se hace clic en el contenido de la celda. También se produce cuando el usuario presiona y suelta la barra espaciadora mientras una celda de botón o una celda de casilla tiene el foco, y se producirá dos veces para estos tipos de celda si se hace clic en el contenido de la celda mientras se presiona la barra ESPACIADORA.

Use este evento para detectar clics de botón para un DataGridViewButtonCell clic de vínculo o para un DataGridViewLinkCell.

Para los clics de un DataGridViewCheckBoxCell, este evento se produce antes de que la casilla cambie el valor, por lo que si no desea calcular el valor esperado en función del valor actual, normalmente controlará el DataGridView.CellValueChanged evento en su lugar. Dado que ese evento solo se produce cuando se confirma el valor especificado por el usuario, que normalmente se produce cuando el foco sale de la celda, también debe controlar el DataGridView.CurrentCellDirtyStateChanged evento. En ese controlador, si la celda actual es una celda de casilla, llame al DataGridView.CommitEdit método y pase el Commit valor.

Para obtener más información acerca de cómo controlar eventos, vea controlar y provocar eventos.

Se aplica a

Consulte también