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

업데이트: 2007년 11월

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

DataGridView.ShowCellToolTips 속성을 false로 설정하여 셀 수준 도구 설명 표시를 비활성화할 수도 있습니다.

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

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

    ' 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 'dataGridView1_CellFormatting
    
    // 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.
    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";
            }
        }
    }
    

코드 컴파일

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

  • 네 개의 별표("*") 기호를 통해 문자열 값을 표시하는 Rating 열을 포함하고 dataGridView1이라는 DataGridView 컨트롤. 컨트롤의 CellFormatting 이벤트를 예제에 표시된 이벤트 처리기 메서드에 연결해야 합니다.

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

강력한 프로그래밍

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

참고 항목

참조

DataGridView

DataGridView.ShowCellToolTips

DataGridView.CellToolTipTextNeeded

DataGridViewCell

DataGridViewCell.ToolTipText

기타 리소스

Windows Forms DataGridView 컨트롤에서 셀, 행 및 열 프로그래밍