次の方法で共有


方法 : Windows フォーム DataGridView コントロールの各セルにツールヒントを追加する

既定では、ツールヒントは、小さすぎて全体の内容を表示できない 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";
            }
        }
    }
    

コードのコンパイル

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

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

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

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

DataGridView コントロールを外部データ ソースにバインドする場合や、仮想モードを実装して独自のデータ ソースを提供する場合には、パフォーマンスが低下することがあります。 大量のデータを処理する際のパフォーマンスの低下を防止するには、複数のセルの ToolTipText プロパティを設定する代わりに、CellToolTipTextNeeded イベントを処理します。 このイベントを処理する場合、セルの ToolTipText プロパティの値を取得するとイベントが発生し、イベント ハンドラーでの指定に基づいて DataGridViewCellToolTipTextNeededEventArgs.ToolTipText プロパティの値が返されます。

参照

参照

DataGridView

DataGridView.ShowCellToolTips

DataGridView.CellToolTipTextNeeded

DataGridViewCell

DataGridViewCell.ToolTipText

その他の技術情報

Windows フォーム DataGridView コントロールのセル、行、および列を使用したプログラミング