DataGridView.CellContentClick DataGridView.CellContentClick DataGridView.CellContentClick DataGridView.CellContentClick Event

정의

셀 내부에 있는 내용을 클릭할 때 발생합니다.Occurs when the content within a cell is clicked.

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 

예제

다음 코드 예제는 클릭 한 셀이 링크 셀 이거나 단추 셀 및 결과적으로 해당 하는 작업을 수행 하는지 여부를 결정 하는이 이벤트에 대 한 처리기를 제공 합니다.The following code example provides a handler for this event that determines whether the clicked cell is a link cell or a button cell and performs the corresponding action as a result. 이 예제는에서 사용할 수 있는 보다 큰 예제의 일부는 DataGridViewComboBoxColumn 클래스 개요 항목입니다.This example is part of a larger example available in the DataGridViewComboBoxColumn class overview topic.

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

설명

이 이벤트는 셀 내용을 클릭 하면 발생 합니다.This event occurs when the cell content is clicked. 사용자 단추 셀 스페이스바를 눌렀다 있고 확인란 셀에 포커스가 셀 내용을 스페이스바를 누른 채 클릭 하는 경우 이러한 셀 형식에 대 한 두 번 발생 하는 경우에 발생 합니다.It also occurs when the user presses and releases the SPACEBAR while a button cell or check box cell has focus, and will occur twice for these cell types if the cell content is clicked while pressing the SPACEBAR.

이 이벤트를 사용 하 여 단추 클릭을 검색 하는 DataGridViewButtonCell 에 대 한 링크를 클릭 하거나 탭 할는 DataGridViewLinkCell합니다.Use this event to detect button clicks for a DataGridViewButtonCell or link clicks for a DataGridViewLinkCell.

클릭을 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.

적용 대상

추가 정보