如何:將工具提示加入至 Windows Form DataGridView 控制項中的個別儲存格

根據預設,Tool提示用來顯示儲存格的值 DataGridView 太小而無法顯示其整個內容。 不過,您可以覆寫此行為,以設定個別儲存格的工具提示文字值。 這適用于向使用者顯示儲存格的其他資訊,或向使用者提供儲存格內容的替代描述。 例如,如果您有顯示狀態圖示的資料列,您可能想要使用 Tool 提供文字說明提示。

您也可以將 屬性設定 DataGridView.ShowCellToolTipsfalse ,以停用資料格層級工具的顯示提示。

將工具提示新增至儲存格

  • 設定 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
    

編譯程式碼

  • 這個範例需要:

  • DataGridView名為 dataGridView1 的控制項,包含名為 Rating 的資料行,用於顯示一到四個星號 (「*」) 符號的字串值。 控制項 CellFormatting 的事件必須與範例中顯示的事件處理常式方法相關聯。

  • SystemSystem.Windows.Forms 組件的參考。

穩固程式設計

當您將 DataGridView 控制項系結至外部資料源,或藉由實作虛擬模式來提供自己的資料來源時,可能會遇到效能問題。 若要避免在處理大量資料時發生效能損失,請處理 CellToolTipTextNeeded 事件,而不是設定 ToolTipText 多個儲存格的 屬性。 當您處理此事件時,取得儲存格 ToolTipText 屬性的值會引發 事件,並傳回 DataGridViewCellToolTipTextNeededEventArgs.ToolTipText 事件處理常式中指定的屬性值。

另請參閱