DataGridView.CellContentClick Událost

Definice

Nastane při kliknutí na obsah v buňce.

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 

Event Type

Příklady

Následující příklad kódu poskytuje obslužnou rutinu pro tuto událost, která určuje, zda je buňka odkazu nebo buňka tlačítka, a jako výsledek provede odpovídající akci. Tento příklad je součástí většího příkladu, který je k dispozici v tématu s přehledem DataGridViewComboBoxColumn třídy.

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

Poznámky

K této události dochází při kliknutí na obsah buňky. Dojde k tomu také v případě, že uživatel stiskne a uvolní mezerník, zatímco buňka tlačítka nebo zaškrtávacího políčka má fokus, a u těchto typů buněk se zobrazí dvakrát, pokud se při stisknutí mezerníku klikne na obsah buňky.

Tato událost slouží k rozpoznání kliknutí na tlačítko nebo DataGridViewButtonCell kliknutí na odkaz pro DataGridViewLinkCell.

U kliknutí v DataGridViewCheckBoxCellsouboru dojde k této události dříve, než zaškrtávací políčko změní hodnotu, takže pokud nechcete vypočítat očekávanou hodnotu na základě aktuální hodnoty, obvykle místo toho zpracujete DataGridView.CellValueChanged událost. Vzhledem k tomu, že k této události dochází pouze v případě, že je potvrzena hodnota zadaná uživatelem, k čemuž obvykle dochází, když fokus opustí buňku, musíte také zpracovat DataGridView.CurrentCellDirtyStateChanged událost. Pokud je aktuální buňka v této obslužné rutině buňkou zaškrtávacího políčka, zavolejte metodu DataGridView.CommitEdit a předejte Commit hodnotu.

Další informace o zpracování událostí najdete v tématu Zpracování a vyvolávání událostí.

Platí pro

Viz také