방법: Windows Forms DataGridView 컨트롤에서 개별 셀에 도구 설명 추가

기본적으로 도구 설명은 전체 내용을 표시하기에는 너무 작은 DataGridView 셀의 값을 표시하는 데 사용됩니다. 그러나 개별 셀에 대해 도구 설명-텍스트 값을 설정하려면 이 동작을 재정의할 수 있습니다. 이것은 사용자에게 셀에 대한 추가 정보를 표시하거나 사용자에게 셀 내용에 대한 대체 설명을 제공하는 데 유용합니다. 예를 들어, 상태 아이콘을 표시하는 행이 있는 경우 도구 설명을 사용하여 텍스트 설명을 제공하려고 할 수 있습니다.

또한 DataGridView.ShowCellToolTips 속성을 false로 설정하여 셀 수준 도구 설명 표시를 사용하지 않도록 설정할 수 있습니다.

셀에 도구 설명을 추가하려면

  • DataGridViewCell.ToolTipText 속성을 설정합니다.

    // Sets the ToolTip text for cells in the Rating column.
    void dataGridView1_CellFormatting(Object^ /*sender*/, 
        DataGridViewCellFormattingEventArgs^ e)
    {
        if ( (e->ColumnIndex == this->dataGridView1->Columns["Rating"]->Index)
            && e->Value != nullptr )
        {
            DataGridViewCell^ cell = 
                this->dataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex];
            if (e->Value->Equals("*"))
            {                
                cell->ToolTipText = "very bad";
            }
            else if (e->Value->Equals("**"))
            {
                cell->ToolTipText = "bad";
            }
            else if (e->Value->Equals("***"))
            {
                cell->ToolTipText = "good";
            }
            else if (e->Value->Equals("****"))
            {
                cell->ToolTipText = "very good";
            }
        }
    }
    
    // Sets the ToolTip text for cells in the Rating column.
    void dataGridView1_CellFormatting(object sender,
        DataGridViewCellFormattingEventArgs e)
    {
        if ( (e.ColumnIndex == this.dataGridView1.Columns["Rating"].Index)
            && e.Value != null )
        {
            DataGridViewCell cell =
                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            if (e.Value.Equals("*"))
            {
                cell.ToolTipText = "very bad";
            }
            else if (e.Value.Equals("**"))
            {
                cell.ToolTipText = "bad";
            }
            else if (e.Value.Equals("***"))
            {
                cell.ToolTipText = "good";
            }
            else if (e.Value.Equals("****"))
            {
                cell.ToolTipText = "very good";
            }
        }
    }
    
    ' Sets the ToolTip text for cells in the Rating column.
    Sub dataGridView1_CellFormatting(ByVal sender As Object, _
        ByVal e As DataGridViewCellFormattingEventArgs) _
        Handles dataGridView1.CellFormatting
    
        If e.ColumnIndex = Me.dataGridView1.Columns("Rating").Index _
            AndAlso (e.Value IsNot Nothing) Then
    
            With Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
    
                If e.Value.Equals("*") Then
                    .ToolTipText = "very bad"
                ElseIf e.Value.Equals("**") Then
                    .ToolTipText = "bad"
                ElseIf e.Value.Equals("***") Then
                    .ToolTipText = "good"
                ElseIf e.Value.Equals("****") Then
                    .ToolTipText = "very good"
                End If
    
            End With
    
        End If
    
    End Sub
    

코드 컴파일

  • 이 예제에는 다음 사항이 필요합니다.

  • 1~4개의 별표(“*”) 기호의 문자열 값을 표시하기 위해 Rating으로 명명된 열을 포함하는 dataGridView1 이름의 DataGridView 컨트롤. 컨트롤의 CellFormatting 이벤트는 예제에 표시된 이벤트 처리기 메서드와 연결되어야 합니다.

  • SystemSystem.Windows.Forms 어셈블리에 대한 참조

강력한 프로그래밍

DataGridView 컨트롤을 외부 데이터 원본에 바인딩하거나 가상 모드를 구현하여 사용자 고유의 데이터 원본을 제공할 때 성능 문제가 발생할 수 있습니다. 많은 양의 데이터로 작업할 때 성능 저하를 방지하려면 여러 셀의 ToolTipText 속성을 설정하는 대신 CellToolTipTextNeeded 이벤트를 처리합니다. 이 이벤트를 처리할 때 셀 ToolTipText 속성의 값을 가져오면 이벤트가 발생하고 이벤트 처리기에 지정된 대로 DataGridViewCellToolTipTextNeededEventArgs.ToolTipText 속성의 값이 반환됩니다.

참고 항목