方法: Windows フォーム 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
    

コードのコンパイル

  • この例で必要な要素は次のとおりです。

  • dataGridView1 という名前の DataGridView コントロール。1 - 4 個のアスタリスク記号 ("*") の文字列値を表示するために、Rating という名前の列が含まれます。 このコントロールの CellFormatting イベントは、例に示すイベント ハンドラー メソッドに関連付けられている必要があります。

  • System アセンブリおよび System.Windows.Forms アセンブリへの参照。

信頼性の高いプログラミング

DataGridView コントロールを外部データ ソースにバインドしたり、仮想モードを実装して独自のデータ ソースを提供したりすると、パフォーマンスの問題が発生する可能性があります。 大量のデータを処理するときにパフォーマンスが低下しないようにするには、複数のセルの ToolTipText プロパティを設定するのではなく、CellToolTipTextNeeded イベントを処理します。 このイベントを処理するときに、セルの ToolTipText プロパティの値を取得すると、イベントが発生し、イベント ハンドラーに指定された DataGridViewCellToolTipTextNeededEventArgs.ToolTipText プロパティの値が返されます。

関連項目