次の方法で共有


方法: 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
    

コードのコンパイル

  • この例では、次のものが必要です。

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

  • System アセンブリと System.Windows.Forms アセンブリへの参照。

堅牢なプログラミング

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

関連項目